minor error messages and linter cleanups

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-30 18:53:09 -07:00
parent a45f3447b7
commit 0f09ba5e87
6 changed files with 36 additions and 27 deletions

View File

@ -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
}

View File

@ -1,4 +1,3 @@
// nolint:dupl
package cmd
import (

View File

@ -1,4 +1,3 @@
// nolint:dupl
package cmd
import (

View File

@ -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

View File

@ -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
}

View File

@ -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",