本周的课程作业是开发一个Agenda项目,由于不要求组队,所以老师简化内容为实现两条指令即可。其中我完成了Register(注册)和login(登陆)两个功能:
- 用户注册
- 注册新用户时,用户需设置一个唯一的用户名和一个密码。另外,还需登记邮箱及电话信息。
- 如果注册时提供的用户名已由其他用户使用,应反馈一个适当的出错信息;成功注册后,亦应反馈一个成功注册的信息。 用户登录
- 用户登录
- 用户使用用户名和密码登录 Agenda 系统。
- 用户名和密码同时正确则登录成功并反馈一个成功登录的信息。否则,登录失败并反馈一个失败登录的信息。
git clone https://github.com/golang/sys
git clone https://github.com/golang/text
执行 go get -v github.com/spf13/cobra/cobra
安装成功后,$GOPATH/bin下会生成cobra可执行程序
2. 创建程序
创建目录App1并初始化:
mkdir -p App1
cd App1
cobra init --pkg-name github.com/github-user/App1
cobra add register
cobra add login
2. 创建一个新的文件夹entity, 存放程序的实体信息
3. rigister.go的内容如下:
package cmd
import (
"App1/entity"
"log"
"github.com/spf13/cobra"
)
var registerCmd = &cobra.Command{
Use: "register",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
_username, _ := cmd.Flags().GetString("user")
_password, _ := cmd.Flags().GetString("password")
_email, _ := cmd.Flags().GetString("email")
_phone, _ := cmd.Flags().GetString("phone")
log.Println("register called by "+_username, _password, _email, _phone)
if _username == "default user" {
log.Println("username can't be empty,plz try again")
return
}
users := entity.READUSERS()
for i := 0; i < len(users); i++ {
if users[i].Username == _username {
log.Println("username already registered")
return
}
}
aUser := entity.User{Username: _username, Password: _password, Email: _email, Phone: _phone}
users = append(users, aUser)
entity.WRITEUSER(users)
},
}
func init() {
rootCmd.AddCommand(registerCmd)
registerCmd.Flags().StringP("user", "u", "default user", "Help message for username")
registerCmd.Flags().StringP("password", "P", "123456", "password for user")
registerCmd.Flags().StringP("email", "e", "[email protected]", "email of user")
registerCmd.Flags().StringP("phone", "p", "18700011134", "phone of the user")
}
login.go:
package cmd
import (
"App1/entity"
"log"
"github.com/spf13/cobra"
)
var loginCmd = &cobra.Command{
Use: "login",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
_userName_, _ := cmd.Flags().GetString("user")
_password_, _ := cmd.Flags().GetString("pass")
users := entity.READUSERS()
for _, user := range users {
if user.Username == _userName_ {
//密码匹配
if user.Password == _password_ {
entity.SetCurrentUserName(_userName_)
log.Println("Log in Success!")
log.Println("Welcome! " + _userName_)
return
} else { //密码错误
log.Println("Warning! Wrong Password")
return
}
}
}
log.Println("Warning! Wrong UserName")
return
},
}
func init() {
rootCmd.AddCommand(loginCmd)
loginCmd.Flags().StringP("user", "u", "", "log in")
loginCmd.Flags().StringP("pass", "p", "", "log in")
}