【Go语言学习】Agenda-cli程序简要说明(只包含对用户操作的指令实现)

写在前面

  • 实验资料在老师的课程网站上发布:传送门
  • 由于完全实现Agenda需要大量时间,所以本次实验是简化版,主要目的是为了熟悉GO语言的io操作和cobra库的使用,实现一个简单的命令行程序。
  • 本次实验项目放到Go-online在线IDE上,分享链接: 传送门

Agenda程序简介

使用简单的命令行程序,能够让用户执行登录、注册等账户操作,并且能够创建会议,选择参加或退出会议等操作。
完整版的Agenda就是一个会议管理系统,但是这次作业,降低了难度,使用了简化版的实现,也就是只实现了部分功能。

使用Corba实现

  • 使用命令 go get -v github.com/spf13/cobra/cobra 下载过程中,会出提示如下错误

    Fetching https://golang.org/x/sys/unix?go-get=1
    https fetch failed: Get https://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
    

这是熟悉的错误,请在 $GOPATH/src/golang.org/x 目录下用 git clone 下载 systext 项目,然后使用 go install github.com/spf13/cobra/cobra, 安装后在 $GOBIN 下出现了 cobra 可执行程序。

Cobra的使用十分简单,直接使用cobra init --pkg-name 创建项目,然后就会有一个你刚刚命名的项目目录,里面有cmd的子目录、main.go文件、LICENSE文件,其中cmd文件下放着各种子命令的实现文件,main就是整个程序的运行文件,比如这次agenda,输入指令就需要先运行agenda,再加上子命令以及参数来运行。
在这里插入图片描述
由以上目录可以看到,本次实现了登录、注册、注销、删除账号、查看账户信息的功能。

entity的目录主要是存放用户操作的逻辑以及数据交互的程序文件,每次执行一个子命令,就会调用entity中的函数,进行逻辑判断如操作是否正确,有无冲突。还需要对交互的信息进行数据存储,如注册用户后,将用户信息存放到json文件中去等

以注册操作为例,简要说明实现

package cmd
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>

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.
*/


import (
	"fmt"

    "git.go-online.org.cn/hsyjkjkl/agenda/entity"
	"github.com/spf13/cobra"
)

// registerCmd represents the register command
var registerCmd = &cobra.Command{
	Use:   "register",
	Short: "regist a new user",
	Long: `register command will help you regist!
	It need you to name a new user and also you should give your email
	your phone number and your password
	the name should not be used before`,

	Run: func(cmd *cobra.Command, args []string) {
		username, _ := cmd.Flags().GetString("username")
		password, _ := cmd.Flags().GetString("password")
		email, _ := cmd.Flags().GetString("email")
		phone, _ := cmd.Flags().GetString("phone")

		s1, s2 := entity.UserRegist(username, password, email, phone)
		if s1 {
			fmt.Printf("Regist new user : \"%v\" successfully!\n", username)
		}
		if s2 {
			fmt.Println("Auto Login...")
		}
	},
}

func init() {
	rootCmd.AddCommand(registerCmd)

	// 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")

	registerCmd.Flags().StringP("username", "u", "", "username not be used")
	registerCmd.Flags().StringP("password", "p", "", "password to login")
	registerCmd.Flags().StringP("email", "e", "", "your email address")
	registerCmd.Flags().StringP("phone", "", "", "your cell phone number")

	// 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")
}

首先可以在当前目录下使用cobra add register 来创建register这个子命令得文件,这时候,就会自动创建一个模板文件,init中定义参数(类似pflags),然后在registerCmd这个结构体中定义相应的信息,如长描述、短描述,更重要的是Run中运行的逻辑,这里我直接调用了entity中的注册函数:

//UserRegist : regist user
func UserRegist(username, password, email, phone string) (bool, bool) {
	state := false
	if username == "" || password == "" || email == "" || phone == "" {
		fmt.Printf("[Regist Failed]: Please ensure you enter all information of register, -u username -p password -e email --phone phoneNumber\n")
		return false, false
	}

	var tmpList []UserInfo
	tmpList = GetAllUsers()

	if UserExisted(username, tmpList) {
		fmt.Printf("[Regist failed]: Username already exited! Please try another one.\n")
		return false, false
	}

	tmp := NewUser(username, password, email, phone)
	tmpList = append(tmpList, tmp)
	SaveAllUser(tmpList)

	if GetCurrentUser() == "" {
		UserLogin(tmp.Username, tmp.Password)
		state = true
	}

	return true, state
}

首先判断是否有完整个人信息,包括用户名、密码、邮箱、电话,其次检测是否存在已注册的同名用户,否则将信息加入到json文件中去,如果当前没有用户在登录,就直接自动登录。返回状态信息。

其他的命令也可以类似的实现,这里涉及到Agenda的逻辑判断问题,之前实训的时候已经做过一次,就不再重复讲了,有兴趣可以参照代码理解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值