Skip to content

Commit

Permalink
Fix Linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DamjanBecirovic committed Oct 10, 2024
1 parent b6d1302 commit dbbf593
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
6 changes: 3 additions & 3 deletions pkg/commands/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (f *File) Extract() error {
absoluteFilePath := filepath.Join(filepath.Dir(f.YamlPath), f.FilePath)

// Open the file
file, err := os.Open(absoluteFilePath)
file, err := os.Open(filepath.Clean((absoluteFilePath)))
if err != nil {
return fmt.Errorf("failed to open the commands_file at %s, error: %w", absoluteFilePath, err)
}
Expand All @@ -39,8 +39,8 @@ func (f *File) Extract() error {

// If no commands were read, return an error indicating that the file is empty
if len(f.Commands) == 0 {
return fmt.Errorf("the commands_file at location %s is empty.", absoluteFilePath)
return fmt.Errorf("the commands_file at location %s is empty", absoluteFilePath)
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/commands/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test__Extract(t *testing.T) {

assert.Error(t, err)

expectedErrorMessage = "the commands_file at location ../../test/fixtures/empty_file.txt is empty."
expectedErrorMessage = "the commands_file at location ../../test/fixtures/empty_file.txt is empty"
assert.Contains(t, err.Error(), expectedErrorMessage)

// Commands are read successfully from the valid file.
Expand Down
39 changes: 26 additions & 13 deletions pkg/pipelines/commands_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,44 @@ func (e *commandsExtractor) extractCommands() error {

func (e *commandsExtractor) updatePipeline() error {
for _, item := range e.files {

cmdFilePath := concatPaths(item.ParentPath, []string{"commands_file"})

err := e.pipeline.raw.Delete(cmdFilePath...)
err := e.deleteCommandsFileField(item)

if err != nil {
return err
}

cmdPath := concatPaths(item.ParentPath, []string{"commands"})

_, err = e.pipeline.raw.Array(cmdPath...)
err = e.addCommands(item)

if err != nil {
return err
}

for _, command := range item.Commands{
e.pipeline.raw.ArrayAppend(command, cmdPath...)
}

if err != nil {
return err
}
}
return nil
}

func (e *commandsExtractor) deleteCommandsFileField(item commands.File) error {
cmdFilePath := concatPaths(item.ParentPath, []string{"commands_file"})

return e.pipeline.raw.Delete(cmdFilePath...)
}

func (e *commandsExtractor) addCommands(item commands.File) error {
cmdPath := concatPaths(item.ParentPath, []string{"commands"})

_, err := e.pipeline.raw.Array(cmdPath...)

if err != nil {
return err
}

for _, command := range item.Commands{
err = e.pipeline.raw.ArrayAppend(command, cmdPath...)

if err != nil {
return err
}
}
return nil
}
2 changes: 2 additions & 0 deletions pkg/pipelines/commands_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
commands "github.com/semaphoreci/spc/pkg/commands"
)

// revive:disable:add-constant

func Test__findAll(t *testing.T) {
yamlPath := "../../test/fixtures/all_commands_file_locations.yml"
pipeline, err := LoadFromFile(yamlPath)
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/cmd_files_all_possible_locations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@
commands_file: "valid_commands_file.txt"
}

commands_file = %{
echo 1
echo 12
echo 123
}
commands_file = %{echo 1\necho 12\necho 123}

origin = TestRepoForChangeIn.setup()

Expand Down

0 comments on commit dbbf593

Please sign in to comment.