Skip to content

Commit

Permalink
Bubble up spec definition errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Mar 11, 2024
1 parent eb00607 commit 6a291a3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/runner/client/client_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,5 @@ func shellID() (int, bool) {
}

func (r *LocalRunner) GetEnvs(ctx context.Context) ([]string, error) {
return r.session.Envs(), nil
return r.session.Envs()
}
6 changes: 5 additions & 1 deletion internal/runner/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ func (c *command) StartWithOpts(ctx context.Context, opts *startOpts) error {
c.Args...,
)
c.cmd.Dir = c.Directory
c.cmd.Env = append(c.cmd.Env, c.Session.Envs()...)
env, err := c.Session.Envs()
if err != nil {
return err
}
c.cmd.Env = append(c.cmd.Env, env...)

if c.tty != nil {
c.cmd.Stdin = c.tty
Expand Down
4 changes: 3 additions & 1 deletion internal/runner/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,16 @@ func Test_command(t *testing.T) {
return s
}

env, err := cmd.Session.Envs()
require.NoError(t, err)
assert.EqualValues(
t,
sort([]string{
"TEST_OLD=value1",
"TEST_OLD_CHANGED=value2",
"TEST_NEW=value2",
}),
sort(cmd.Session.Envs()),
sort(env),
)
})

Expand Down
34 changes: 27 additions & 7 deletions internal/runner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ func newRunnerService(logger *zap.Logger) (*runnerService, error) {
}, nil
}

func toRunnerv1Session(sess *Session) *runnerv1.Session {
func toRunnerv1Session(sess *Session) (*runnerv1.Session, error) {
env, err := sess.Envs()
if err != nil {
return nil, err
}
return &runnerv1.Session{
Id: sess.ID,
Envs: sess.Envs(),
Envs: env,
Metadata: sess.Metadata,
}
}, nil
}

func (r *runnerService) CreateSession(ctx context.Context, req *runnerv1.CreateSessionRequest) (*runnerv1.CreateSessionResponse, error) {
Expand Down Expand Up @@ -104,8 +108,12 @@ func (r *runnerService) CreateSession(ctx context.Context, req *runnerv1.CreateS

r.logger.Debug("created session", zap.String("id", sess.ID))

runnerSess, err := toRunnerv1Session(sess)
if err != nil {
return nil, err
}
return &runnerv1.CreateSessionResponse{
Session: toRunnerv1Session(sess),
Session: runnerSess,
}, nil
}

Expand All @@ -118,8 +126,12 @@ func (r *runnerService) GetSession(_ context.Context, req *runnerv1.GetSessionRe
return nil, status.Error(codes.NotFound, "session not found")
}

runnerSess, err := toRunnerv1Session(sess)
if err != nil {
return nil, err
}
return &runnerv1.GetSessionResponse{
Session: toRunnerv1Session(sess),
Session: runnerSess,
}, nil
}

Expand All @@ -133,7 +145,11 @@ func (r *runnerService) ListSessions(_ context.Context, req *runnerv1.ListSessio

runnerSessions := make([]*runnerv1.Session, 0, len(sessions))
for _, s := range sessions {
runnerSessions = append(runnerSessions, toRunnerv1Session(s))
runnerSess, err := toRunnerv1Session(s)
if err != nil {
return nil, err
}
runnerSessions = append(runnerSessions, runnerSess)
}

return &runnerv1.ListSessionsResponse{Sessions: runnerSessions}, nil
Expand Down Expand Up @@ -659,7 +675,11 @@ func (r *runnerService) getProgramResolverFromReq(req *runnerv1.ResolveProgramRe
// Add session env as a source.
session, found := r.getSessionFromRequest(req)
if found {
sources = append(sources, commandpkg.ProgramResolverSourceFunc(session.Envs()))
env, err := session.Envs()
if err != nil {
return nil, err
}
sources = append(sources, commandpkg.ProgramResolverSourceFunc(env))
}

mode := commandpkg.ProgramResolverModeAuto
Expand Down
27 changes: 14 additions & 13 deletions internal/runner/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var owlStoreDefault = false

type envStorer interface {
envs() []string
envs() ([]string, error)
addEnvs(envs []string) error
updateStore(envs []string, newOrUpdated []string, deleted []string) error
setEnv(k string, v string) error
Expand Down Expand Up @@ -75,9 +75,12 @@ func (s *Session) SetEnv(k string, v string) error {
return s.envStorer.setEnv(k, v)
}

func (s *Session) Envs() []string {
vals := s.envStorer.envs()
return vals
func (s *Session) Envs() ([]string, error) {
vals, err := s.envStorer.envs()
if err != nil {
return nil, err
}
return vals, nil
}

func (s *Session) Subscribe(ctx context.Context, snapshotc chan<- owl.SetVarItems) error {
Expand All @@ -102,7 +105,7 @@ type SessionList struct {
}

type runnerEnvStorer struct {
logger *zap.Logger
// logger *zap.Logger
envStore *envStore
}

Expand All @@ -126,13 +129,12 @@ func (es *runnerEnvStorer) addEnvs(envs []string) error {
return nil
}

func (es *runnerEnvStorer) envs() []string {
func (es *runnerEnvStorer) envs() ([]string, error) {
envs, err := es.envStore.Values()
if err != nil {
es.logger.Error("failed to get envs", zap.Error(err))
return nil
return nil, err
}
return envs
return envs, nil
}

func (es *runnerEnvStorer) setEnv(k string, v string) error {
Expand Down Expand Up @@ -307,13 +309,12 @@ func (es *owlEnvStorer) setEnv(k string, v string) error {
return err
}

func (es *owlEnvStorer) envs() []string {
func (es *owlEnvStorer) envs() ([]string, error) {
vals, err := es.owlStore.InsecureValues()
if err != nil {
es.logger.Error("failed to get vals", zap.Error(err))
return nil
return nil, err
}
return vals
return vals, nil
}

func NewSessionList() (*SessionList, error) {
Expand Down

0 comments on commit 6a291a3

Please sign in to comment.