cmd

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	App = &cli.App{
		Name:      "ipgw",
		HelpName:  "ipgw",
		Copyright: "Home page:\thttps://github.com/neucn/ipgw\nFeedback:\thttps://github.com/neucn/ipgw/issues/new",
		Commands: []*cli.Command{
			LoginCommand,
			LogoutCommand,
			KickCommand,
			InfoCommand,
			ConfigCommand,
			TestCommand,
			VersionCommand,
			UpdateCommand,
		},
		Action: func(ctx *cli.Context) error {
			if ctx.NArg() != 0 {
				console.InfoL("command not found\n")
				cli.ShowAppHelpAndExit(ctx, 1)
				return nil
			}
			return loginUseDefaultAccount(ctx)
		},
		HideVersion: true,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "secret",
				Aliases: []string{"s"},
				Hidden:  true,
			},
			&cli.StringFlag{
				Name:    "config",
				Aliases: []string{"f"},
				Usage:   "load configuration from specific `file`",
			},
		},
		OnUsageError: onUsageError,
	}
)
View Source
var (
	ConfigCommand = &cli.Command{
		Name:  "config",
		Usage: "manage config",
		Subcommands: []*cli.Command{
			{
				Name:  "account",
				Usage: "manage accounts stored in config",
				Subcommands: []*cli.Command{
					configAccountAddCommand,
					configAccountDelCommand,
					configAccountSetCommand,
					configAccountListCommand,
				},
				OnUsageError: onUsageError,
			},
		},
		OnUsageError: onUsageError,
	}
)
View Source
var (
	InfoCommand = &cli.Command{
		Name:                   "info",
		Usage:                  "list account info",
		UseShortOptionHandling: true,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "username",
				Aliases: []string{"u"},
				Usage:   "student number `id` (required only if not use the default or first stored account)",
			},
			&cli.StringFlag{
				Name:    "password",
				Aliases: []string{"p"},
				Usage:   "`password` for pass.neu.edu.cn (required only if account is not stored)",
			},
			&cli.StringFlag{
				Name:    "secret",
				Aliases: []string{"s"},
				Usage:   "`secret` for stored account (required only if secret is not empty)",
			},
			&cli.BoolFlag{Name: "all", Aliases: []string{"a"}, Usage: "list all kind of info, equivalent to -lbird"},

			&cli.BoolFlag{Name: "package", Aliases: []string{"i"}, Usage: "print campus network package info"},
			&cli.BoolFlag{Name: "device", Aliases: []string{"d"}, Usage: "print logged-in devices"},
			&cli.IntFlag{Name: "recharge", Aliases: []string{"r"}, Value: 1, Usage: "print the specific `page` of recharge records"},
			&cli.IntFlag{Name: "bill", Aliases: []string{"b"}, Value: 1, Usage: "print the specific `page` of bills"},
			&cli.IntFlag{Name: "log", Aliases: []string{"l"}, Value: 1, Usage: "print the specific `page` of usage logs"},
		},
		Action: func(ctx *cli.Context) error {
			account, err := getAccountByContext(ctx)
			if err != nil {
				return err
			}
			h := handler.NewDashboardHandler()
			if err := h.Login(account); err != nil {
				return fmt.Errorf("fail to login:\n\t%v", err)
			}
			processInfoPrint(ctx, &infoPrinter{h})
			return nil
		},
		OnUsageError: onUsageError,
	}
)
View Source
var (
	KickCommand = &cli.Command{
		Name:                   "kick",
		Usage:                  "logout any specific device by SID",
		ArgsUsage:              "[sid list]",
		UseShortOptionHandling: true,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "username",
				Aliases: []string{"u"},
				Usage:   "student number `id` (required only if not use the default or first stored account)",
			},
			&cli.StringFlag{
				Name:    "password",
				Aliases: []string{"p"},
				Usage:   "`password` for pass.neu.edu.cn (required only if account is not stored)",
			},
			&cli.StringFlag{
				Name:    "secret",
				Aliases: []string{"s"},
				Usage:   "`secret` for stored account (required only if secret is not empty)",
			},
		},
		Action: func(ctx *cli.Context) error {
			sids := ctx.Args().Slice()
			if len(sids) == 0 {
				console.InfoL("no sid")
				return nil
			}
			account, err := getAccountByContext(ctx)
			if err != nil {
				return err
			}
			h := handler.NewIpgwHandler()
			password, err := account.GetPassword()
			if err != nil {
				return err
			}
			if err = h.NEUAuth(account.Username, password); err != nil {
				return err
			}
			for _, sid := range sids {
				result, err := h.Kick(sid)
				if result {
					console.InfoF("#%s: done\n", sid)
				} else {
					console.InfoF("#%s: fail\n", sid)
					if err != nil {
						console.InfoF("\t%v\n", err)
					}
				}
			}
			return nil
		},
	}
)
View Source
var (
	LoginCommand = &cli.Command{
		Name:  "login",
		Usage: "login ipgw",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "username",
				Aliases: []string{"u"},
				Usage:   "student number `id` (required only if not use the default or first stored account)",
			},
			&cli.StringFlag{
				Name:    "password",
				Aliases: []string{"p"},
				Usage:   "`password` for pass.neu.edu.cn (required only if account is not stored)",
			},
			&cli.StringFlag{
				Name:    "cookie",
				Aliases: []string{"c"},
				Usage:   "`cookie` item 'session_for%3Asrun_cas_php' from ipgw.neu.edu.cn",
			},
			&cli.StringFlag{
				Name:    "secret",
				Aliases: []string{"s"},
				Usage:   "`secret` for stored account (required only if secret is not empty)",
			},
			&cli.BoolFlag{
				Name:    "info",
				Aliases: []string{"i"},
				Usage:   "output account info after login successfully",
			},
		},
		Action: func(ctx *cli.Context) error {
			account, err := getAccountByContext(ctx)
			if err != nil {
				return err
			}
			h := handler.NewIpgwHandler()
			if err = login(h, account); err != nil {
				return fmt.Errorf("login failed: \n\t%v", err)
			}
			if ctx.Bool("info") {
				if err = h.FetchUsageInfo(); err != nil {
					return fmt.Errorf("fetch info failed: \n\t%v", err)
				}
				info := h.GetInfo()
				console.InfoF("\tIP\t%16s\n\t余额\t%16s\n\t流量\t%16s\n\t时长\t%16s\n",
					info.IP,
					info.FormattedBalance(),
					info.FormattedTraffic(),
					info.FormattedUsedTime())
			}
			return nil
		},
		OnUsageError: onUsageError,
	}
)
View Source
var (
	LogoutCommand = &cli.Command{
		Name:  "logout",
		Usage: "logout ipgw",
		Action: func(ctx *cli.Context) error {
			h := handler.NewIpgwHandler()
			connected, loggedIn := h.IsConnectedAndLoggedIn()
			if !connected {
				return errors.New("not in campus network")
			}
			if !loggedIn {
				return errors.New("not logged in yet")
			}
			info := h.GetInfo()
			if err := h.Logout(); err != nil {
				return fmt.Errorf("fail to logout account '%s':\n\t%v", info.Username, err)
			}
			console.InfoF("logout account '%s' successfully\n", info.Username)
			return nil
		},
		OnUsageError: onUsageError,
	}
)
View Source
var (
	TestCommand = &cli.Command{
		Name:  "test",
		Usage: "test whether is connected to the campus network and whether has logged in ipgw",
		Action: func(ctx *cli.Context) error {
			h := handler.NewIpgwHandler()
			connected, loggedIn := h.IsConnectedAndLoggedIn()
			console.Info("campus network:   ")
			if connected {
				console.InfoL("connected")
			} else {
				console.InfoL("disconnected")
			}
			console.Info("ipgw logged in:   ")
			if loggedIn {
				console.InfoL("yes")
			} else {
				console.InfoL("no")
			}
			return nil
		},
		OnUsageError: onUsageError,
	}
)
View Source
var (
	UpdateCommand = &cli.Command{
		Name:  "update",
		Usage: "check latest version of ipgw and update",
		Action: func(ctx *cli.Context) error {
			h := handler.NewUpdateHandler()
			newer, err := h.CheckLatestVersion()
			if err != nil {
				return err
			}
			if !newer {
				console.InfoL("already the latest version")
				return nil
			}
			err = h.Update()
			if err != nil {
				return fmt.Errorf("fail to update:\n\t%v", err)
			}
			console.InfoL("update successfully")
			return nil
		},
	}
)
View Source
var (
	VersionCommand = &cli.Command{
		Name:  "version",
		Usage: "show version and build info",
		Action: func(ctx *cli.Context) error {
			console.InfoF("ipgw %s+%s\n", ipgw.Version, ipgw.Build)
			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL