服务计算——Agenda命令设计

来源:互联网 发布:淘宝开店冲话费 编辑:程序博客网 时间:2024/04/27 20:56

1. cobra安装

  1. 首先需要安装git,如已经安装则可忽略这步。只需打开终端,输入下面命令即可。(ubuntu系统)

    apt-get install git

  2. 使用命令 go get -v github.com/spf13/cobra/cobra下载过程中,会出提示如下错误(估计是和墙有关)

Fetching https://golang.org/x/sys/unix?go-get=1https 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 下载 sys 和 text 项目,然后使用 go install github.com/spf13/cobra/cobra, 安装后在 $GOBIN 下出现了 cobra 可执行程序。

或者走个捷径,点击此处直接下载缺少的文件,解压后放在GoPath的SRC文件中。(感谢stary_yan提供的文件)

2. cobra的简单使用

在src文件夹打开终端,输入以下命令来生成agenda项目并创建一个命令文件register

cobra init agenda
cobra add register

接着在register.go文件中的 init()函数添加

registerCmd.Flags().StringP(“user”, “u”, “Anonymous”, “Help message for username”)

在Run 匿名回调函数中添加

username, _ := cmd.Flags().GetString(“user”)
fmt.Println(“register called by ” + username)

最后在终端输入,即可解析命令行的参数,以下输出显然已经解析成功

$ go run main.go register –user=TestUser
register called by TestUser

3. 根据需求,设计agenda命令

用户注册

  1. 注册新用户时,用户需设置一个唯一的用户名和一个密码。另外,还需登记邮箱及电话信息。
  2. 如果注册时提供的用户名已由其他用户使用,应反馈一个适当的出错信息;成功注册后,亦应反馈一个成功注册的信息。

用户登录

  1. 用户使用用户名和密码登录 Agenda 系统。
  2. 用户名和密码同时正确则登录成功并反馈一个成功登录的信息。否则,登录失败并反馈一个失败登录的信息。

用户登出

  1. 已登录的用户登出系统后,只能使用用户注册和用户登录功能。

用户查询

  1. 已登录的用户可以查看已注册的所有用户的用户名、邮箱及电话信息。

用户删除

  1. 已登录的用户可以删除本用户账户(即销号)。
  2. 操作成功,需反馈一个成功注销的信息;否则,反馈一个失败注销的信息。
  3. 删除成功则退出系统登录状态。删除后,该用户账户不再存在。
  4. 用户账户删除以后:
    • 以该用户为 发起者 的会议将被删除
    • 以该用户为 参与者 的会议将从 参与者 列表中移除该用户。若因此造成会议 参与者 人数为0,则会议也将被删除。

创建会议

  1. 已登录的用户可以添加一个新会议到其议程安排中。会议可以在多个已注册
    用户间举行,不允许包含未注册用户。添加会议时提供的信息应包括:
    • 会议主题(title)(在会议列表中具有唯一性)
    • 会议参与者(participator)
    • 会议起始时间(start time)
    • 会议结束时间(end time)
  2. 注意,任何用户都无法分身参加多个会议。如果用户已有的会议安排(作为发起者或参与者)与将要创建的会议在时间上重叠 (允许仅有端点重叠的情况),则无法创建该会议。
  3. 用户应获得适当的反馈信息,以便得知是成功地创建了新会议,还是在创建过程中出现了某些错误。

增删会议参与者

  1. 已登录的用户可以向 自己发起的某一会议增加/删除 参与者 。
  2. 增加参与者时需要做 时间重叠 判断(允许仅有端点重叠的情况)。
  3. 删除会议参与者后,若因此造成会议 参与者 人数为0,则会议也将被删除。

查询会议

  1. 已登录的用户可以查询自己的议程在某一时间段(time interval)内的所有会议安排。
  2. 用户给出所关注时间段的起始时间和终止时间,返回该用户议程中在指定时间范围内找到的所有会议安排的列表。
  3. 在列表中给出每一会议的起始时间、终止时间、主题、以及发起者和参与者。
  4. 注意,查询会议的结果应包括用户作为 发起者或参与者 的会议。

取消会议

  1. 已登录的用户可以取消 自己发起 的某一会议安排。
  2. 取消会议时,需提供唯一标识:会议主题(title)。

退出会议

  1. 已登录的用户可以退出 自己参与 的某一会议安排。
  2. 退出会议时,需提供一个唯一标识:会议主题(title)。若因此造成会议 参与者 人数为0,则会议也将被删除。

清空会议

  1. 已登录的用户可以清空 自己发起 的所有会议安排

分析需求之后通过,cobra将需要的命令添加到cmd文件夹中,最后的设计如下:

 ./agendayou can use this app to create or remove meetings.Also you must register a user to have the rights to use the functions.Usage:  agenda [command]Available Commands:  add         To add Participator of the meeting  clear       To clear all the meeting created by the current user  create      To create a new meeting  delete      To delete your account in Agenda  deleteM     delete meeting with the title [title]  help        Help about any command  login       Using UserName with PassWord to login Agenda.  logout      To logout Agenda  queryM      To query all the meeting have attended during [StartTime] and [EndTime]  queryU      To query all the users' names  quit        quit the meeting with the title [title]  register    Register a new user  remove      To remove Participator from the meetingFlags:      --config string   config file (default is $HOME/.agenda.yaml)  -h, --help            help for agenda  -t, --toggle          Help message for toggleUse "agenda [command] --help" for more information about a command.

各个命令的用法和功能:

register(注册用户)

agenda register -u [UserName] -p [Pass] -e [Email] -t [Phone]

./agenda register -hInput command register -u UserName -p PassWord -e Email -t Phone:[Username] is the name of the new register[PassWord] is the password to login[Email]is the email address of the register[Phone] is the phone of the registerUsage:  agenda register -u [UserName] -p [Pass] -e [Email] -t [Phone] [flags]Flags:  -h, --help   help for registerGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

login(用户登录)

agenda login -u [UserName] -p [PassWord]

./agenda login -hUsing UserName and PassWord to login Agenda:attention:If the PassWord is right,you can login Agenda and use itIf forget the PassWord,you must register another one UserUsage:  agenda login -u [UserName] -p [PassWord] [flags]Flags:  -h, --help   help for loginGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

logout(当前用户登出)

agenda logout

 ./agenda logout -hAfter logouting,you can only register or login:register -u [UserName] -p [Pass] -e [Email]login -u [UserName] -p [PassWord]Usage:  agenda logout [flags]Flags:  -h, --help   help for logoutGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

queryU(查看已注册的所有用户的用户名、邮箱及电话信息)

agenda queryU

 ./agenda queryU -hYou can query all the users's names who have registed.Usage:  agenda queryU [flags]Flags:  -h, --help   help for queryUGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

delete(删除本用户)

agenda delete

./agenda delete -hyou can delete your account in the database of Agenda:attention:After deleting,you will need to register a new User to login Agenda.Usage:  agenda delete [flags]Flags:  -h, --help   help for deleteGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

create(创建会议)

agenda create -t [Title] -p [Participator] -s [StartTime] -e [EndTime]

 ./agenda create -hTo create a new meeting with:[Title] the Title of the meeting[Participator] the Participator of the meeting,the Participator can only attend one meeting during one meeting time[StartTime] the StartTime of the meeting[EndTime] the EndTime of the meetingUsage:  agenda create -t [Title] -p [Participator] -s [StartTime] -e [EndTime] [flags]Flags:  -h, --help   help for createGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

add(添加会议参与者)

agenda add -p [Participator] -t [Title]

/agenda$ ./agenda add -hAdd [Participator] to the meeting with the title of [Title]:attention:If the Participator cannot attend during the time, add fail.Usage:  agenda add -p [Participator] -t [Title] [flags]Flags:  -h, --help   help for addGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

remove(从某会议删除某参与者)

agenda remove -p [Participator] -t [Title]

 ./agenda remove -hremove [Participator] from the meeting with the title of [Title]:attention:If there is no Participators in the meeting,the meeting will be deletedUsage:  agenda remove -p [Participator] -t [Title] [flags]Flags:  -h, --help   help for removeGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

queryM(查询会议)

agenda queryM -s [StartTime] -e [EndTime]

./agenda queryM -hYou can query all the meeting have attended during [StartTime] and [EndTime]Usage:  agenda queryM -s [StartTime] -e [EndTime] [flags]Flags:  -h, --help   help for queryMGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

deleteM(根据会议名删除会议)

agenda delete -t [title]

./agenda deleteM -hyou can delete one meeting with the title [title]Usage:  agenda deleteM -t [title] [flags]Flags:  -h, --help   help for deleteMGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

quit(当前用户退出会议)

agenda quit -t [title]

./agenda quit -hyou can quit the meeting with the title of [title]:attention:if there is no participators in this meeting,the meeting will be deletedUsage:  agenda quit -t [title] [flags]Flags:  -h, --help   help for quitGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)

clear(清除当前用户创建的会议)

agenda clear

./agenda clearYou can delete all the meeting created by youUsage:  agenda clear [flags]Flags:  -h, --help   help for clearGlobal Flags:      --config string   config file (default is $HOME/.agenda.yaml)
原创粉丝点击