diff --git a/cmd/dagger/cmd/common/common.go b/cmd/dagger/cmd/common/common.go index 155d9133..c7f3f527 100644 --- a/cmd/dagger/cmd/common/common.go +++ b/cmd/dagger/cmd/common/common.go @@ -17,7 +17,11 @@ func GetCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route { route, err := dagger.NewRoute(st) if err != nil { - lg.Fatal().Err(err).Interface("routeState", st).Msg("failed to init route") + lg. + Fatal(). + Err(err). + Interface("routeState", st). + Msg("failed to init route") } return route @@ -26,29 +30,31 @@ func GetCurrentRoute(ctx context.Context, store *dagger.Store) *dagger.Route { func GetCurrentRouteState(ctx context.Context, store *dagger.Store) *dagger.RouteState { lg := log.Ctx(ctx) - var ( - st *dagger.RouteState - err error - ) - routeName := viper.GetString("route") if routeName != "" { - st, err = store.LookupRouteByName(ctx, routeName) + st, err := store.LookupRouteByName(ctx, routeName) if err != nil { - lg.Fatal().Err(err).Str("routeName", routeName).Msg("failed to lookup route by name") - } - } else { - wd, err := os.Getwd() - if err != nil { - lg.Fatal().Err(err).Msg("cannot get current working directory") - } - - st, err = store.LookupRouteByPath(ctx, wd) - if err != nil { - lg.Fatal().Err(err).Str("routePath", wd).Msg("failed to lookup route by path") + lg. + Fatal(). + Err(err). + Str("routeName", routeName). + Msg("failed to lookup route by name") } + return st } + wd, err := os.Getwd() + if err != nil { + lg.Fatal().Err(err).Msg("cannot get current working directory") + } + st, err := store.LookupRouteByPath(ctx, wd) + if err != nil { + lg. + Fatal(). + Err(err). + Str("routePath", wd). + Msg("failed to lookup route by path") + } return st } diff --git a/cmd/dagger/cmd/down.go b/cmd/dagger/cmd/down.go index cce8255e..35aa7d89 100644 --- a/cmd/dagger/cmd/down.go +++ b/cmd/dagger/cmd/down.go @@ -1,4 +1,3 @@ -// nolint:dupl package cmd import ( diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index d47bbb6a..bffb7722 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -1,4 +1,3 @@ -// nolint:dupl package cmd import ( diff --git a/dagger/route.go b/dagger/route.go index b2013b1b..788cfc3c 100644 --- a/dagger/route.go +++ b/dagger/route.go @@ -143,7 +143,7 @@ func (r *Route) State() *compiler.Value { // LoadLayout loads the layout func (r *Route) LoadLayout(ctx context.Context, s Solver) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "r.Update") + span, ctx := opentracing.StartSpanFromContext(ctx, "route.Update") defer span.Finish() layoutSource, err := r.st.LayoutSource.Compile() @@ -185,7 +185,6 @@ func (r *Route) LocalDirs() map[string]string { if err != nil { return err } - // nolint:goconst // FIXME: merge Env into Route, or fix the linter error if do != "local" { return nil diff --git a/dagger/store.go b/dagger/store.go index 50e604d7..4566126b 100644 --- a/dagger/store.go +++ b/dagger/store.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "os" "path" "sync" @@ -11,6 +12,11 @@ import ( "github.com/google/uuid" ) +var ( + ErrRouteExist = errors.New("route already exists") + ErrRouteNotExist = errors.New("route doesn't exist") +) + const ( defaultStoreRoot = "$HOME/.config/dagger/routes" ) @@ -144,7 +150,7 @@ func (s *Store) CreateRoute(ctx context.Context, st *RouteState) error { defer s.l.Unlock() if _, ok := s.routesByName[st.Name]; ok { - return os.ErrExist + return fmt.Errorf("%s: %w", st.Name, ErrRouteExist) } st.ID = uuid.New().String() @@ -179,7 +185,7 @@ func (s *Store) LookupRouteByID(ctx context.Context, id string) (*RouteState, er st, ok := s.routes[id] if !ok { - return nil, os.ErrNotExist + return nil, fmt.Errorf("%s: %w", id, ErrRouteNotExist) } return st, nil } @@ -190,7 +196,7 @@ func (s *Store) LookupRouteByName(ctx context.Context, name string) (*RouteState st, ok := s.routesByName[name] if !ok { - return nil, os.ErrNotExist + return nil, fmt.Errorf("%s: %w", name, ErrRouteNotExist) } return st, nil } @@ -201,7 +207,7 @@ func (s *Store) LookupRouteByPath(ctx context.Context, path string) (*RouteState st, ok := s.routesByPath[path] if !ok { - return nil, os.ErrNotExist + return nil, fmt.Errorf("%s: %w", path, ErrRouteNotExist) } return st, nil } diff --git a/dagger/store_test.go b/dagger/store_test.go index cc279773..9602a336 100644 --- a/dagger/store_test.go +++ b/dagger/store_test.go @@ -19,7 +19,7 @@ func TestStoreLoad(t *testing.T) { _, err = store.LookupRouteByName(ctx, "notexist") require.Error(t, err) - require.True(t, errors.Is(err, os.ErrNotExist)) + require.True(t, errors.Is(err, ErrRouteNotExist)) st := &RouteState{ Name: "test",