Skip to content

Commit

Permalink
fix: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Aug 20, 2024
1 parent 534b88a commit 94856df
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions pkg/controller/outputshell/output_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ import (
func (c *Controller) OutputShell(ctx context.Context, logE *logrus.Entry, param *config.Param) error { //nolint:funlen

Check failure on line 21 in pkg/controller/outputshell/output_shell.go

View workflow job for this annotation

GitHub Actions / test / test

directive `//nolint:funlen` is unused for linter "funlen" (nolintlint)
shellPath := filepath.Join(c.rootDir, "shell", strconv.Itoa(param.Ppid), "shell.json")

oldShell := &Shell{}
if err := c.readShell(shellPath, oldShell); err != nil {
oldPaths, err := c.readOldPaths(shellPath)
if err != nil {
return err
}

oldPaths := make(map[string]struct{}, len(oldShell.GetPaths()))
for _, p := range oldShell.GetPaths() {
oldPaths[p] = struct{}{}
}

shell := &Shell{
Env: &Env{
Path: &Path{
Expand All @@ -55,6 +50,33 @@ func (c *Controller) OutputShell(ctx context.Context, logE *logrus.Entry, param
}
}

newPS, updated := c.getNewPS(param, shell, oldPaths)

if updated {
fmt.Fprintln(c.stdout, "export PATH="+strings.Join(newPS, param.PathListSeparator))
}

if err := c.saveShell(shellPath, shell); err != nil {
return err
}

return nil
}

func (c *Controller) readOldPaths(shellPath string) (map[string]struct{}, error) {
oldShell := &Shell{}
if err := c.readShell(shellPath, oldShell); err != nil {
return nil, err
}

oldPaths := make(map[string]struct{}, len(oldShell.GetPaths()))
for _, p := range oldShell.GetPaths() {
oldPaths[p] = struct{}{}
}
return oldPaths, nil
}

func (c *Controller) getNewPS(param *config.Param, shell *Shell, oldPaths map[string]struct{}) ([]string, bool) {
paths := make(map[string]struct{}, len(shell.GetPaths()))
for _, p := range shell.GetPaths() {
paths[p] = struct{}{}
Expand Down Expand Up @@ -90,16 +112,7 @@ func (c *Controller) OutputShell(ctx context.Context, logE *logrus.Entry, param
}
newPS = append(newPS, p)
}

if updated {
fmt.Fprintln(c.stdout, "export PATH="+strings.Join(newPS, param.PathListSeparator))
}

if err := c.saveShell(shellPath, shell); err != nil {
return err
}

return nil
return newPS, updated
}

func (c *Controller) saveShell(shellPath string, shell *Shell) error {
Expand Down Expand Up @@ -187,11 +200,11 @@ func (c *Controller) handlePkg(shell *Shell, pkg *config.Package) error {
}
newP, err := pkg.RenderTemplateString(p, c.runtime)
if err != nil {
return err
return fmt.Errorf("render added $PATH: %w", err)
}
pkgPath, err := pkg.PkgPath(c.rootDir, c.runtime)
if err != nil {
return err
return fmt.Errorf("get the installed package path: %w", err)
}
shell.Env.Path.Values = append(shell.Env.Path.Values, filepath.Join(pkgPath, filepath.FromSlash(newP)))
return nil
Expand Down

0 comments on commit 94856df

Please sign in to comment.