Skip to content

Commit 4de8f56

Browse files
authored
Merge pull request #214 from cmbahadir/singlePullRequest
Added Index numbers for compose files to be used in "compose list/set"
2 parents 4cc7fc7 + e8ab806 commit 4de8f56

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
.ci/
2-
build/
2+
build/

cmd/sourced/cmd/compose.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"strconv"
56

67
composefile "github.com/src-d/sourced-ce/cmd/sourced/compose/file"
78

@@ -51,7 +52,8 @@ func (c *composeListCmd) Execute(args []string) error {
5152
return err
5253
}
5354

54-
for _, file := range files {
55+
for index, file := range files {
56+
fmt.Printf("[%d]", index)
5557
if file == active {
5658
fmt.Printf("* %s\n", file)
5759
} else {
@@ -66,16 +68,34 @@ type composeSetDefaultCmd struct {
6668
Command `name:"set" short-description:"Set the active docker compose file" long-description:"Set the active docker compose file"`
6769

6870
Args struct {
69-
Version string `positional-arg-name:"version" description:"Either a revision (tag, full sha1) or a URL to a docker-compose.yml file"`
71+
File string `positional-arg-name:"index/name" description:"Provide name or index of compose file on 'sourced compose list'"`
7072
} `positional-args:"yes" required:"yes"`
7173
}
7274

7375
func (c *composeSetDefaultCmd) Execute(args []string) error {
74-
err := composefile.SetActive(c.Args.Version)
76+
files, err := composefile.List()
77+
7578
if err != nil {
7679
return err
7780
}
7881

82+
index, err := strconv.Atoi(c.Args.File)
83+
84+
if err == nil {
85+
if index >= 0 && index < len(files) {
86+
active := files[index]
87+
err = composefile.SetActive(active)
88+
} else {
89+
return fmt.Errorf("File not found with provided index check the output of 'sourced compose list'")
90+
}
91+
92+
} else if err != nil {
93+
err := composefile.SetActive(c.Args.File)
94+
if err != nil {
95+
return err
96+
}
97+
}
98+
7999
fmt.Println("Active docker compose file was changed successfully.")
80100
fmt.Println("To update your current installation use `sourced restart`")
81101
return nil

test/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func (s *IntegrationSuite) SetupTest() {
5454
p, _ := filepath.Abs(filepath.FromSlash("../docker-compose.yml"))
5555
os.Symlink(p, filepath.Join(s.sourcedDir, "compose-files", "local", "docker-compose.yml"))
5656

57-
r := s.RunCommand("compose", "set", "local")
57+
//"0" refers to local
58+
r := s.RunCommand("compose", "set", "0")
5859
s.Require().NoError(r.Error, r.Combined())
5960
}
6061

test/init_local_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"strings"
1011
"testing"
1112

13+
"github.com/stretchr/testify/assert"
1214
"github.com/stretchr/testify/require"
1315
"github.com/stretchr/testify/suite"
1416
)
@@ -41,6 +43,60 @@ func (s *InitLocalTestSuite) TestWithInvalidWorkdir() {
4143
)
4244
}
4345

46+
func (s *InitLocalTestSuite) TestListComposeFiles() {
47+
list := "list"
48+
s.T().Run(list, func(t *testing.T) {
49+
assert := assert.New(t)
50+
51+
r := s.RunCommand("compose", list)
52+
stdOut := r.Stdout()
53+
check := strings.Contains(stdOut, "[0] local")
54+
assert.True(check)
55+
})
56+
}
57+
58+
func (s *InitLocalTestSuite) TestSetComposeFile() {
59+
set := "set"
60+
s.T().Run(set, func(t *testing.T) {
61+
assert := assert.New(t)
62+
63+
r := s.RunCommand("compose", set, "0")
64+
stdOut := r.Stdout()
65+
check := strings.Contains(stdOut, "Active docker compose file was changed successfully")
66+
assert.True(check)
67+
})
68+
}
69+
70+
func (s *InitLocalTestSuite) TestSetComposeFilIndexOutOfRange() {
71+
set := "set"
72+
s.T().Run(set, func(t *testing.T) {
73+
assert := assert.New(t)
74+
75+
r := s.RunCommand("compose", set, "5")
76+
stdErr := r.Stderr()
77+
check := strings.Contains(stdErr, "File not found with provided index check the output of 'sourced compose list'")
78+
assert.True(check)
79+
})
80+
}
81+
82+
func (s *InitLocalTestSuite) TestSetComposeNotFound() {
83+
require := s.Require()
84+
r := s.RunCommand("compose", "set", "NotFound")
85+
require.Error(r.Error)
86+
}
87+
88+
func (s *InitLocalTestSuite) TestSetComposeFilesWithStringIndex() {
89+
set := "set"
90+
s.T().Run(set, func(t *testing.T) {
91+
assert := assert.New(t)
92+
93+
r := s.RunCommand("compose", set, "local")
94+
stdOut := r.Stdout()
95+
check := strings.Contains(stdOut, "Active docker compose file was changed successfully")
96+
assert.True(check)
97+
})
98+
}
99+
44100
func (s *InitLocalTestSuite) TestChangeWorkdir() {
45101
req := s.Require()
46102

0 commit comments

Comments
 (0)