Skip to content

Commit

Permalink
1:33:36]
Browse files Browse the repository at this point in the history
  • Loading branch information
vouv committed Nov 1, 2020
1 parent 237e45d commit 49abe97
Show file tree
Hide file tree
Showing 82 changed files with 12,428 additions and 162 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

## Update Log

2020.11.1
2020.11.2

- 优化新版登陆逻辑
- 优化命令行框架
- 删除无用代码

2020.9.6
Expand Down
125 changes: 36 additions & 89 deletions cmd/srun/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"bufio"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vouv/srun/core"
"github.com/vouv/srun/pkg/term"
"github.com/vouv/srun/store"
Expand All @@ -14,75 +14,53 @@ import (
"strings"
)

var ErrReadAccount = errors.New("读取账号文件错误, 请执行`srun config`配置账号信息")

type Func func(cmd string, params ...string)

var DefaultClient = &Client{}

type Client struct{}

// 登录
func (s *Client) Login(cmd string, params ...string) {
account, gErr := store.ReadAccount()
if gErr != nil {
log.Error(ErrReadAccount.Error())
log.Debug(gErr)
os.Exit(1)
func LoginE(cmd *cobra.Command, args []string) error {
account, err := store.ReadAccount()
if err != nil {
return err
}

log.Info("尝试登录...")

//username = model.AddSuffix(username, server)
info, err := core.Login(&account)
if err != nil {
log.Error(err)
os.Exit(1)
return err
}
log.Info("登录成功!")

err = store.SetInfo(info.AccessToken, info.ClientIp)
err = store.SetInfo(info.AccessToken, info.Acid)
if err != nil {
log.Error(err)
os.Exit(1)
return err
}

s.GetInfo(cmd, params...)
//GetInfo(cmd, params...)
return nil
}

func (s *Client) Logout(cmd string, params ...string) {
if len(params) == 0 {
var err error
account, err := store.ReadAccount()
if err != nil {
log.Error(ErrReadAccount.Error())
log.Debug(err)
os.Exit(1)
}
if err = core.Logout(account.Username); err != nil {
log.Error(err)
os.Exit(1)
}
log.Info("注销成功!")
} else {
s.CmdList()
func LogoutE(cmd *cobra.Command, args []string) error {
var err error
account, err := store.ReadAccount()
if err != nil {
return err
}
if err = core.Logout(account.Username); err != nil {
return err
}
log.Info("注销成功!")
return nil
}

func (s *Client) GetInfo(cmd string, params ...string) {
if len(params) == 0 {
res, err := core.Info()
if err != nil {
log.Error(err)
os.Exit(1)
}
fmt.Println(res.String())
} else {
s.CmdList()
func InfoE(cmd *cobra.Command, args []string) error {
res, err := core.Info()
if err != nil {
log.Error(err)
os.Exit(1)
}
fmt.Println(res.String())
return nil
}

func (Client) SetAccount(cmd string, params ...string) {
func ConfigE(cmd *cobra.Command, args []string) error {

in := os.Stdin
fmt.Print("设置校园网账号:\n>")
Expand Down Expand Up @@ -113,6 +91,7 @@ func (Client) SetAccount(cmd string, params ...string) {
os.Exit(1)
}
log.Info("账号密码已被保存")
return nil
}

func readInput(in io.Reader) string {
Expand All @@ -124,47 +103,15 @@ func readInput(in io.Reader) string {
return string(line)
}

func (Client) ShowVersion() {
fmt.Println("System:")
fmt.Printf("\tOS:%s ARCH:%s GOVERSION:%s\n", runtime.GOOS, runtime.GOARCH, runtime.Version())
fmt.Println("About:")
fmt.Printf("\tVersion: %s\n", Version)
fmt.Println("\n\t</> with ❤ By vouv")
}

// srun help [COMMAND]
func (s *Client) CmdHelp(cmd string, params ...string) {
if len(params) == 0 {
fmt.Println(s.CmdList())
} else {
if c, ok := cmdDocs[params[0]]; ok {
fmt.Println("Usage: ", c[0])
} else {
fmt.Println(s.CmdList())
}
}
}

func (Client) CmdList() string {
sb := &strings.Builder{}
sb.WriteString("Srun " + Version + "\r\n")
sb.WriteString(fmt.Sprint("\r\nUsage: srun [OPTIONS] COMMAND \r\n\r\n"))

sb.WriteString("A efficient client for BIT campus network\r\n\r\n")

sb.WriteString("Options:\r\n")
for k, v := range optionDocs {
sb.WriteString(fmt.Sprintf(" %-10s%-20s\r\n", k, v))
}

sb.WriteString("\r\nCommands:\r\n")
for k, v := range cmdDocs {
sb.WriteString(fmt.Sprintf(" %-10s%-20s\r\n", k, v[1]))
}
return sb.String()
func VersionString() string {
return fmt.Sprintln("System:") +
fmt.Sprintf("\tOS:%s ARCH:%s GO:%s\n", runtime.GOOS, runtime.GOARCH, runtime.Version()) +
fmt.Sprintln("About:") +
fmt.Sprintf("\tVersion: %s\n", Version) +
fmt.Sprintln("\n\t</> with ❤ By vouv")
}

func (Client) Update(cmd string, params ...string) {
func Update(cmd string, params ...string) {
ok, v, d := HasUpdate()
if !ok {
log.Info("当前已是最新版本:", Version)
Expand Down
105 changes: 42 additions & 63 deletions cmd/srun/main.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,72 @@
package main

import (
"flag"
log "github.com/sirupsen/logrus"
log2 "log"
"net/http"
"github.com/spf13/cobra"
"os"
"time"
)

const (
Version = "v0.1.24"
Timeout = 3 * time.Second
)

var LogLevel = log.InfoLevel
const Version = "v0.1.25"

var CommandMap = map[string]Func{
"config": DefaultClient.SetAccount,
"login": DefaultClient.Login,
"logout": DefaultClient.Logout,
"info": DefaultClient.GetInfo,
"update": DefaultClient.Update,
var loginCmd = &cobra.Command{
Use: "login",
Short: "login srun",
RunE: LoginE,
}

"help": DefaultClient.CmdHelp,
var logoutCmd = &cobra.Command{
Use: "logout",
Short: "logout srun",
RunE: LogoutE,
}

var optionDocs = map[string]string{
"-d": "Show debug message",
"-v": "Print version information and quit",
"-h": "Show help",
var infoCmd = &cobra.Command{
Use: "info",
Short: "get srun info",
RunE: InfoE,
}

var cmdDocs = map[string][]string{
"config": {"srun config", "Set Username and Password"},
"login": {"srun [login]", "Login Srun"},
"logout": {"srun logout", "Logout Srun"},
"info": {"srun info", "Get Srun Info"},
"update": {"srun update", "Update srun"},
var configCmd = &cobra.Command{
Use: "config",
Short: "config srun",
RunE: ConfigE,
}

func main() {
var debugMode bool
var helpMode bool
var versionMode bool
var rootCmd = &cobra.Command{
Use: "srun [command]",
Short: "A efficient client for BIT campus network",
RunE: func(cmd *cobra.Command, args []string) error {
if debugMode {
log.SetLevel(log.DebugLevel)
}
return LoginE(cmd, args)
},
}

flag.BoolVar(&debugMode, "d", false, "debug mode")
flag.BoolVar(&helpMode, "h", false, "show help")
flag.BoolVar(&versionMode, "v", false, "show version")
var debugMode bool

flag.Parse()
func main() {

var cmd string
var params []string
rootCmd.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "debug mode")

args := flag.Args()
if len(args) > 0 {
cmd = args[0]
params = args[1:]
} else {
cmd = "login"
}
// version
rootCmd.Version = Version
rootCmd.SetVersionTemplate(VersionString())

switch {
case helpMode:
DefaultClient.CmdHelp(cmd, args...)
return
case versionMode:
DefaultClient.ShowVersion()
return
case debugMode:
LogLevel = log.DebugLevel
}
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(logoutCmd)
rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(configCmd)

// config
http.DefaultClient.Timeout = Timeout
log2.SetOutput(nil)
log.SetOutput(os.Stdout)
log.SetLevel(LogLevel)
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{
//DisableTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})

if handle, ok := CommandMap[cmd]; ok {
handle(cmd, params...)
} else {
DefaultClient.CmdHelp(cmd, params...)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.1.1
golang.org/x/sys v0.0.0-20191115151921-52ab43148777 // indirect
)
Loading

0 comments on commit 49abe97

Please sign in to comment.