cli: cleanup logger usage

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-25 16:07:39 -07:00
parent a3b84386bc
commit f59b30a27c
7 changed files with 80 additions and 24 deletions

View File

@ -7,15 +7,21 @@ import (
"path/filepath"
"dagger.io/go/dagger"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
// getRouteName returns the selected route name (based on explicit CLI selection or current work dir)
func getRouteName(lg zerolog.Logger, cmd *cobra.Command) string {
func getRouteName(ctx context.Context, cmd *cobra.Command) string {
lg := log.Ctx(ctx)
routeName, err := cmd.Flags().GetString("route")
if err != nil {
lg.Fatal().Err(err).Str("flag", "route").Msg("unable to resolve flag")
lg.
Fatal().
Err(err).
Str("flag", "route").
Msg("unable to resolve flag")
}
if routeName != "" {
@ -24,7 +30,10 @@ func getRouteName(lg zerolog.Logger, cmd *cobra.Command) string {
workDir, err := os.Getwd()
if err != nil {
lg.Fatal().Err(err).Msg("failed to get current working dir")
lg.
Fatal().
Err(err).
Msg("failed to get current working dir")
}
currentDir := filepath.Base(workDir)
@ -35,7 +44,9 @@ func getRouteName(lg zerolog.Logger, cmd *cobra.Command) string {
return currentDir
}
func routeUp(ctx context.Context, lg zerolog.Logger, route *dagger.Route) {
func routeUp(ctx context.Context, route *dagger.Route) {
lg := log.Ctx(ctx)
c, err := dagger.NewClient(ctx, "")
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")

View File

@ -31,7 +31,7 @@ var computeCmd = &cobra.Command{
lg := logger.New()
ctx := lg.WithContext(cmd.Context())
name := getRouteName(lg, cmd)
name := getRouteName(ctx, cmd)
st := &dagger.RouteState{
ID: uuid.New().String(),
Name: name,
@ -43,7 +43,11 @@ var computeCmd = &cobra.Command{
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.TextInput(v))
if err != nil {
lg.Fatal().Err(err).Str("input", k).Msg("failed to add input")
lg.
Fatal().
Err(err).
Str("input", k).
Msg("failed to add input")
}
}
@ -52,7 +56,11 @@ var computeCmd = &cobra.Command{
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.DirInput(v, []string{}))
if err != nil {
lg.Fatal().Err(err).Str("input", k).Msg("failed to add input")
lg.
Fatal().
Err(err).
Str("input", k).
Msg("failed to add input")
}
}
@ -61,7 +69,11 @@ var computeCmd = &cobra.Command{
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.GitInput(v, "", ""))
if err != nil {
lg.Fatal().Err(err).Str("input", k).Msg("failed to add input")
lg.
Fatal().
Err(err).
Str("input", k).
Msg("failed to add input")
}
}
@ -120,7 +132,7 @@ var computeCmd = &cobra.Command{
lg.Fatal().Err(err).Msg("unable to initialize route")
}
routeUp(ctx, lg, route)
routeUp(ctx, route)
},
}

View File

@ -25,15 +25,24 @@ var downCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(lg, cmd)
routeName := getRouteName(ctx, cmd)
route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route")
lg.
Fatal().
Err(err).
Str("routeName", routeName).
Msg("failed to lookup route")
}
// TODO: Implement options: --no-cache
if err := route.Down(ctx, nil); err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Str("route-id", route.ID()).Msg("failed to up the route")
lg.
Fatal().
Err(err).
Str("routeName", routeName).
Str("routeId", route.ID()).
Msg("failed to up the route")
}
},
}

View File

@ -27,7 +27,10 @@ var listCmd = &cobra.Command{
routes, err := store.ListRoutes(ctx)
if err != nil {
lg.Fatal().Err(err).Msg("cannot list routes")
lg.
Fatal().
Err(err).
Msg("cannot list routes")
}
for _, name := range routes {

View File

@ -26,20 +26,28 @@ var newCmd = &cobra.Command{
upRouteFlag, err := cmd.Flags().GetBool("up")
if err != nil {
lg.Fatal().Err(err).Str("flag", "up").Msg("unable to resolve flag")
lg.
Fatal().
Err(err).
Str("flag", "up").
Msg("unable to resolve flag")
}
routeName := getRouteName(lg, cmd)
routeName := getRouteName(ctx, cmd)
// TODO: Implement options: --layout-*, --setup
route, err := store.CreateRoute(ctx, routeName, nil)
if err != nil {
lg.Fatal().Err(err).Msg("failed to create route")
}
lg.Info().Str("route-id", route.ID()).Str("route-name", routeName).Msg("created route")
lg.
Info().
Str("routeId", route.ID()).
Str("routeName", routeName).
Msg("route created")
if upRouteFlag {
routeUp(ctx, lg, route)
routeUp(ctx, route)
}
},
}

View File

@ -26,17 +26,26 @@ var queryCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(lg, cmd)
routeName := getRouteName(ctx, cmd)
route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route")
lg.
Fatal().
Err(err).
Str("routeName", routeName).
Msg("failed to lookup route")
}
expr := args[0]
out, err := route.Query(ctx, expr, nil)
if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Str("route-id", route.ID()).Msg("failed to query route")
lg.
Fatal().
Err(err).
Str("routeName", routeName).
Str("routeId", route.ID()).
Msg("failed to query route")
}
fmt.Println(out)

View File

@ -25,14 +25,18 @@ var upCmd = &cobra.Command{
ctx := lg.WithContext(cmd.Context())
store := dagger.DefaultStore()
routeName := getRouteName(lg, cmd)
routeName := getRouteName(ctx, cmd)
route, err := store.LookupRoute(ctx, routeName, nil)
if err != nil {
lg.Fatal().Err(err).Str("route-name", routeName).Msg("failed to lookup route")
lg.
Fatal().
Err(err).
Str("routeName", routeName).
Msg("failed to lookup route")
}
// TODO: Implement options: --no-cache
routeUp(ctx, lg, route)
routeUp(ctx, route)
},
}