update cli

Signed-off-by: Nathan13888 <29968201+Nathan13888@users.noreply.github.com>
This commit is contained in:
Nathan Chung 2023-06-04 16:21:41 -04:00
parent 8df4892b7e
commit 4d50dc1f4a
Signed by: n4chung
SSH Key Fingerprint: SHA256:/+NqYA5MfQIjjfwa4z16mw3Y+lxgY/Ml8wCeGnh6qBU
1 changed files with 51 additions and 7 deletions

58
main.go
View File

@ -1,15 +1,29 @@
package main package main
import ( import (
"log"
"os" "os"
"path/filepath"
"github.com/Nathan13888/mirror-checker2/v2/checkers"
"github.com/Nathan13888/mirror-checker2/v2/config" "github.com/Nathan13888/mirror-checker2/v2/config"
"github.com/Nathan13888/mirror-checker2/v2/web" "github.com/Nathan13888/mirror-checker2/v2/web"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
func getAbsPath(path string) string {
absPath, err := filepath.Abs(path)
if err != nil {
log.Error().Err(err).Msg("Failed to get absolute path.")
return path
}
return absPath
}
func main() { func main() {
var err error
config.SetupLogger(true) // TODO: flag for debug mode
// Start CLI // Start CLI
// TODO: documentation - https://cli.urfave.org/v2/examples/full-api-example/ // TODO: documentation - https://cli.urfave.org/v2/examples/full-api-example/
app := &cli.App{ app := &cli.App{
@ -18,14 +32,25 @@ func main() {
Version: config.BuildVersion, Version: config.BuildVersion,
EnableBashCompletion: true, EnableBashCompletion: true,
// https://cli.urfave.org/v2/examples/combining-short-options/ // https://cli.urfave.org/v2/examples/combining-short-options/
// TODO: flags for config file (mirrors.json) and input // TODO: flags for config file (mirrors.json), defaults to mirrors.json
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "server", Name: "daemon",
Aliases: []string{"s", "serve", "web"}, Aliases: []string{"s", "d", "serve", "web", "server"},
Usage: "starts web API", Usage: "starts web API",
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
// TODO: flags for port, listen address // TODO: flags for port, listen address
// TODO: flag --config or --stdin-config
// TODO: enable all projects by default
// checkers.LoadDefaultProjects()
path := getAbsPath("data/mirrors.json")
log.Info().Str("path", path).Msg("Loading config file.")
err = config.LoadFromFile(path)
if err != nil {
log.Fatal().Err(err).Msg("Failed to load config file.")
}
return web.StartServer() return web.StartServer()
}, },
@ -33,8 +58,27 @@ func main() {
{ {
Name: "check", Name: "check",
Aliases: []string{"c"}, Aliases: []string{"c"},
Usage: "checks particular mirror", Usage: "checks particular mirror (once)",
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
checkers.LoadDefaultProjects()
// verify all projects are enabled
// TODO:
// attempt to look up and check all projects
for _, arg := range cCtx.Args().Slice() {
log.Printf("\nPulling project information for '%s'\n\n", arg)
proj, err := checkers.GetProject(arg)
if err != nil {
log.Fatal().Err(err).
Str("project", arg).
Msg("Failed to load project.")
return err
}
proj.RunChecksAsync()
}
return nil return nil
}, },
// TODO: auto complete available mirrors, https://cli.urfave.org/v2/examples/combining-short-options/ // TODO: auto complete available mirrors, https://cli.urfave.org/v2/examples/combining-short-options/
@ -44,7 +88,7 @@ func main() {
// return // return
// } // }
// for _, t := range config.Mirrors { // for _, t := range config.Mirrors {
// fmt.Println(t) // log.Println(t)
// } // }
// }, // },
}, },
@ -52,6 +96,6 @@ func main() {
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
log.Fatal(err) log.Fatal().Err(err).Msg("An error occurred.")
} }
} }