Skip to content

Commit f83b560

Browse files
authored
Add ensure repositories subcommand (#21)
Issue N/A Description of changes: This patch introduces a new subcommand to ensure that configured services or core repositories are forked and cloned locally. In addition to that, this patch also fixes a sneaky bug that was preventing `ackdev list repos` from showing non-cloned repositories. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent dcd1167 commit f83b560

File tree

6 files changed

+139
-20
lines changed

6 files changed

+139
-20
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,48 @@ kustomize OK v4.0.1 /usr/local/bin/kustomize
179179
controller-gen OK v0.4.0 /usr/bin/controller-gen
180180
```
181181

182+
#### Managed repositories
183+
184+
`ackdev` can help manage the repositories you need to interact with in your ACK
185+
development journey. To list all the repositories that you have configured, you
186+
can run:
187+
188+
```bash
189+
ackdev list repos # repo|repository|repositories [--filter|--show-branch]
190+
```
191+
192+
The output will look like this:
193+
```bash
194+
NAME TYPE BRANCH
195+
test-infra core main
196+
runtime core main
197+
code-generator core main
198+
dev-tools core main
199+
dynamodb-controller controller main
200+
ecr-controller controller main
201+
s3-controller controller main
202+
eks-controller controller main
203+
sqs-controller controller main
204+
s3-controller controller main
205+
mq-controller controller main
206+
elasticache-controller controller main
207+
```
208+
209+
You can filter repositories by name, type, branch or name prefix. e.g `--filter=type=controller`
210+
211+
To configure and ensure (fork+clone) a new repository you can run:
212+
213+
```bash
214+
ackdev add repo eks --type=controller
215+
```
216+
217+
To ensure that all the configured repositories are forked in your github account
218+
and cloned in your local GOPATH, you can run:
219+
220+
```bash
221+
ackdev ensure repos
222+
```
223+
182224
## License
183225

184226
This project is licensed under the Apache-2.0 License.

cmd/ackdev/cmd/ensure.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package cmd
15+
16+
import (
17+
"github.com/spf13/cobra"
18+
)
19+
20+
func init() {
21+
ensureCmd.AddCommand(ensureRepositoriesCmd)
22+
}
23+
24+
var ensureCmd = &cobra.Command{
25+
Use: "ensure",
26+
Args: cobra.NoArgs,
27+
Short: "Ensure repositories or dependencies",
28+
}

cmd/ackdev/cmd/ensure_repos.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package cmd
15+
16+
import (
17+
"github.com/spf13/cobra"
18+
19+
"github.com/aws-controllers-k8s/dev-tools/pkg/config"
20+
"github.com/aws-controllers-k8s/dev-tools/pkg/repository"
21+
)
22+
23+
var ensureRepositoriesCmd = &cobra.Command{
24+
Use: "repo",
25+
Aliases: []string{"repos", "repositories", "repository"},
26+
RunE: ensureAllRepositories,
27+
Args: cobra.NoArgs,
28+
Short: "Ensure repositories are forked and cloned locally",
29+
}
30+
31+
func ensureAllRepositories(cmd *cobra.Command, args []string) error {
32+
cfg, err := config.Load(ackConfigPath)
33+
if err != nil {
34+
return err
35+
}
36+
37+
repoManager, err := repository.NewManager(cfg)
38+
if err != nil {
39+
return err
40+
}
41+
42+
err = repoManager.LoadAll()
43+
if err != nil {
44+
return err
45+
}
46+
47+
ctx := cmd.Context()
48+
return repoManager.EnsureAll(ctx)
49+
}

cmd/ackdev/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func init() {
3232
rootCmd.AddCommand(addCmd)
3333
rootCmd.AddCommand(versionCmd)
3434
rootCmd.AddCommand(setupCmd)
35+
rootCmd.AddCommand(ensureCmd)
3536
}
3637

3738
var rootCmd = &cobra.Command{

pkg/repository/manager.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,29 @@ func (m *Manager) AddRepository(name string, t RepositoryType) (*Repository, err
116116
repo := NewRepository(name, t)
117117

118118
// set expected fork name
119-
expectedForkName := repo.Name
119+
repo.ExpectedForkName = repo.Name
120120
if m.cfg.Github.ForkPrefix != "" {
121-
expectedForkName = fmt.Sprintf("%s%s", m.cfg.Github.ForkPrefix, repo.Name)
121+
repo.ExpectedForkName = fmt.Sprintf("%s%s", m.cfg.Github.ForkPrefix, repo.Name)
122122
}
123123

124-
var gitHead string
125-
var gitRepo *git.Repository
126-
fullPath := filepath.Join(m.cfg.RootDirectory, repo.Name)
124+
repo.FullPath = filepath.Join(m.cfg.RootDirectory, repo.Name)
125+
gitRepo, err := m.git.Open(repo.FullPath)
126+
if err == git.ErrRepositoryNotExists {
127+
m.repoCache[name] = repo
128+
return repo, nil
129+
}
130+
if err != nil {
131+
return nil, err
132+
}
127133

128-
gitRepo, err := m.git.Open(fullPath)
129-
if err != nil && err != git.ErrRepositoryNotExists {
134+
// load current branch
135+
head, err := gitRepo.Head()
136+
if err != nil {
130137
return nil, err
131-
} else if err == nil {
132-
// load current branch
133-
head, err := gitRepo.Head()
134-
if err != nil {
135-
return nil, err
136-
}
137-
gitHead = head.Name().Short()
138138
}
139139

140140
repo.gitRepo = gitRepo
141-
repo.GitHead = gitHead
142-
repo.FullPath = fullPath
143-
repo.ExpectedForkName = expectedForkName
141+
repo.GitHead = head.Name().Short()
144142
// cache repository
145143
m.repoCache[name] = repo
146144
return repo, nil
@@ -157,8 +155,7 @@ func (m *Manager) LoadAll() error {
157155
}
158156
}
159157
for _, serviceName := range m.cfg.Repositories.Services {
160-
serviceRepoName := fmt.Sprintf("%s-controller", serviceName)
161-
_, err := m.LoadRepository(serviceRepoName, RepositoryTypeController)
158+
_, err := m.LoadRepository(serviceName, RepositoryTypeController)
162159
if err != nil {
163160
return err
164161
}

pkg/repository/manager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ func TestManager_LoadAll(t *testing.T) {
164164
git: fakeGit,
165165
repoCache: make(map[string]*Repository),
166166
},
167-
wantErr: true,
167+
// `ackdev list repositories` should not hide non cloned repositories.
168+
// It should just show that there are no active branches locally.
169+
wantErr: false,
168170
},
169171
{
170172
name: "unexpected repository error",

0 commit comments

Comments
 (0)