本次Agenda是一次简化式的实验,因此仅要求实现两条指令,我选择的是实现agenda register
以及agenda login
这两条指令
本次实验要求采用cobra实现,因此,需要首先安装cobra,可通过以下指令安装:
go get -v github.com/spf13/cobra/cobra
成功安装后,要注意,在go-online中,cobra的位置,我们可以首先使用echo $GOPATH
查看GOPATH的位置,如下:
cobra 就位于/home/go-online/go-online-dep/bin/cobra
中
初始化指令如下:
/home/go-online/go-online-dep/bin/cobra init --pkg-name agenda
添加相应的指令:
/home/go-online/go-online-dep/bin/cobra add register
/home/go-online/go-online-dep/bin/cobra add login
在agenda/cmd/register.go
中,我们将代码完善如下:
/*
Copyright © 2019 NAME HERE
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"git.go-online.org.cn/henryumi/agenda/entity"
"github.com/spf13/cobra"
)
// registerCmd represents the register command
var registerCmd = &cobra.Command{
Use: "register",
Short: "resister",
Long: "agenda registr -u [username] -p [password] -e [email] -h [phone]",
Run: func(cmd *cobra.Command, args []string) {
//fmt.Println("register called")
usern,_ := cmd.Flags().GetString("username")
pass,_ := cmd.Flags().GetString("password")
email,_ := cmd.Flags().GetString("email")
phone,_ := cmd.Flags().GetString("phone")
state := entity.Register(usern,pass,email,phone)
if state == 0{
fmt.Println("A same username have been used")
}else if(state == 1){
fmt.Println("register succeed")
// entity.saveFile()
}else if(state == 2){
fmt.Println("agenda registr -u [username] -p [password] -e [email] -h [phone]")
}
},
}
func init() {
rootCmd.AddCommand(registerCmd)
entity.ReadFile()
registerCmd.Flags().StringP("username","u","","")
registerCmd.Flags().StringP("password","p","","")
registerCmd.Flags().StringP("email","e","","")
registerCmd.Flags().StringP("phone","t","","")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// registerCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// registerCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
在init
函数中,使用Flags绑定参数,在registerCmd中,实现具体的函数,通过调用我在agenda/entity/file.go
中定义的注册函数来实现用户注册功能,并且通过返回值判断是否正确注册,若发生错误,则输出错误的内容。
在agenda/cmd/login.go
中,我们将代码完善如下:
/*
Copyright © 2019 NAME HERE
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"os"
"fmt"
"git.go-online.org.cn/henryumi/agenda/entity"
"github.com/spf13/cobra"
)
// loginCmd represents the login command
var loginCmd = &cobra.Command{
Use: "login",
Short: "login",
Long: "agenda login -u [username] -p [password]",
Run: func(cmd *cobra.Command, args []string) {
//fmt.Println("login called")
usern,_ := cmd.Flags().GetString("username")
pass,_ := cmd.Flags().GetString("password")
state := entity.Login(usern,pass)
if state == 0{
fmt.Println("The user doesn't exit")
}else if(state == 1){
fmt.Fprintf(os.Stdout,"%s login succeed\n",usern)
// entity.saveFile()
}else if(state == 2){
fmt.Println("the username can't match with the password")
}
},
}
func init() {
rootCmd.AddCommand(loginCmd)
entity.ReadFile()
loginCmd.Flags().StringP("username","u","","")
loginCmd.Flags().StringP("password","p","","")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// loginCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// loginCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
这部分的实现与上文的register相似,只是绑定的参数,以及run中调用的函数发生变化
这一部分我认为是本次实验的主要内容,负责了注册以及登陆的逻辑功能
注册函数如下
func Register(name,passw,mail,phone string) int{
var user User
user.Username = name
user.Password = passw
user.Email = mail
user.Phone = phone
if name == ""{
return 2
}
if is_exits(&user){
return 0
}else{
total = append(total,user)
saveFile()
return 1
}
}
该函数通过判断是否已有已注册同名用户来判断是否允许注册,判断函数如下:
func is_exits(user * User) bool{
for i := 0;i < len(total);i ++{
if total[i].Username == user.Username{
return true
}
}
return false
}
login登陆函数如下:
func Login(name,passw string) int{
var user User
user.Username = name
user.Password = passw
if !is_exits(&user){
return 0
}else{
if is_true(&user){
return 1
}else{
return 2
}
}
}
该登陆函数,需要两次判断,一次判断用户是否存在,第二次判断若存在,密码是否匹配,以此来进行错误反馈,判断密码是否匹配的函数如下:
func is_true(user *User) bool{
for i := 0;i < len(total);i ++{
if total[i].Username == user.Username{
if(total[i].Password == user.Password){
return true
}else{
return false
}
}
}
return false
}
以上即为本次实验的主要内容
注意,在go-online上的程序想要正确运行,需要先执行su
指令,并输入密码
首先是注册功能,我先登陆一个不存在的用户:
提示并不存在该用户
进行注册:
注册成功,尝试登陆:
提示成功登陆
故意输入错误的密码:
得到了相应的错误提示
测试完毕