Skip to content

Commit d210806

Browse files
author
Nicholas Thomson
committed
Added the add repository command
1 parent e822b6a commit d210806

File tree

5 files changed

+118
-52
lines changed

5 files changed

+118
-52
lines changed

cmd/ackdev/cmd/add.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 "github.com/spf13/cobra"
17+
18+
func init() {
19+
addCmd.AddCommand(addRepositoryCmd)
20+
}
21+
22+
var addCmd = &cobra.Command{
23+
Use: "add",
24+
Aliases: []string{},
25+
Args: cobra.NoArgs,
26+
Short: "Creates one or more resources",
27+
}

cmd/ackdev/cmd/add_repo.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
"errors"
18+
"fmt"
19+
"strings"
20+
21+
"github.com/aws-controllers-k8s/dev-tools/pkg/config"
22+
"github.com/aws-controllers-k8s/dev-tools/pkg/repository"
23+
"github.com/aws-controllers-k8s/dev-tools/pkg/util"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
var (
28+
ErrRepositoryAlreadyAdded = errors.New("repository has already been added")
29+
)
30+
31+
var addRepositoryCmd = &cobra.Command{
32+
Use: "repository <service> ...",
33+
Aliases: []string{"repo", "repos", "repository", "repositories"},
34+
RunE: addRepository,
35+
Args: cobra.MinimumNArgs(1),
36+
}
37+
38+
func addRepository(cmd *cobra.Command, args []string) error {
39+
for _, service := range args {
40+
service = strings.ToLower(args[0])
41+
42+
ctx := cmd.Context()
43+
44+
cfg, err := config.Load(ackConfigPath)
45+
if err != nil {
46+
return err
47+
}
48+
49+
// Check it doesn't already exist in the configuration
50+
if util.InStrings(service, cfg.Repositories.Services) {
51+
return ErrRepositoryAlreadyAdded
52+
}
53+
54+
repoManager, err := repository.NewManager(cfg)
55+
if err != nil {
56+
return err
57+
}
58+
59+
serviceRepoName := fmt.Sprintf("%s-controller", service)
60+
if err := repoManager.EnsureRepository(ctx, serviceRepoName); err != nil {
61+
return err
62+
}
63+
64+
cfg.Repositories.Services = append(cfg.Repositories.Services, service)
65+
config.Save(cfg, ackConfigPath)
66+
}
67+
68+
return nil
69+
}

cmd/ackdev/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func init() {
2929

3030
rootCmd.AddCommand(listCmd)
3131
rootCmd.AddCommand(editCmd)
32+
rootCmd.AddCommand(addCmd)
3233
rootCmd.AddCommand(versionCmd)
3334
rootCmd.AddCommand(setupCmd)
3435
}

pkg/repository/manager.go

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"context"
1818
"errors"
1919
"fmt"
20-
"os"
2120
"path/filepath"
21+
"strings"
2222
"time"
2323

2424
"github.com/sirupsen/logrus"
@@ -29,7 +29,6 @@ import (
2929
"github.com/aws-controllers-k8s/dev-tools/pkg/config"
3030
ackdevgit "github.com/aws-controllers-k8s/dev-tools/pkg/git"
3131
"github.com/aws-controllers-k8s/dev-tools/pkg/github"
32-
"github.com/aws-controllers-k8s/dev-tools/pkg/util"
3332
)
3433

3534
const (
@@ -90,30 +89,14 @@ type Manager struct {
9089
}
9190

9291
// LoadRepository loads information about a single local repository
93-
func (m *Manager) LoadRepository(name string, t RepositoryType) (*Repository, error) {
92+
func (m *Manager) LoadRepository(name string) (*Repository, error) {
9493
// check repo cache
95-
repo, err := m.GetRepository(name)
94+
repo, err := m.getRepository(name)
9695
if err == nil {
9796
return repo, nil
9897
}
9998

100-
// fail if repository doesn't exist in the manager configuration
101-
switch t {
102-
case RepositoryTypeCore:
103-
if !util.InStrings(name, m.cfg.Repositories.Core) {
104-
return nil, ErrUnconfiguredRepository
105-
}
106-
case RepositoryTypeController:
107-
if !util.InStrings(name, m.cfg.Repositories.Services) {
108-
return nil, ErrUnconfiguredRepository
109-
}
110-
}
111-
11299
repoName := name
113-
// controller repositories should always have a '-controller' suffix
114-
if t == RepositoryTypeController {
115-
repoName = fmt.Sprintf("%s-controller", name)
116-
}
117100

118101
// set expected fork name
119102
expectedForkName := repoName
@@ -137,6 +120,11 @@ func (m *Manager) LoadRepository(name string, t RepositoryType) (*Repository, er
137120
gitHead = head.Name().Short()
138121
}
139122

123+
t := RepositoryTypeCore
124+
if strings.HasSuffix(name, "-controller") {
125+
t = RepositoryTypeController
126+
}
127+
140128
repo = &Repository{
141129
Name: repoName,
142130
Type: t,
@@ -156,22 +144,23 @@ func (m *Manager) LoadRepository(name string, t RepositoryType) (*Repository, er
156144
func (m *Manager) LoadAll() error {
157145
// collect repositories from config
158146
for _, coreRepo := range m.cfg.Repositories.Core {
159-
_, err := m.LoadRepository(coreRepo, RepositoryTypeCore)
147+
_, err := m.LoadRepository(coreRepo)
160148
if err != nil {
161149
return err
162150
}
163151
}
164152
for _, serviceName := range m.cfg.Repositories.Services {
165-
_, err := m.LoadRepository(serviceName, RepositoryTypeController)
153+
serviceRepoName := fmt.Sprintf("%s-controller", serviceName)
154+
_, err := m.LoadRepository(serviceRepoName)
166155
if err != nil {
167156
return err
168157
}
169158
}
170159
return nil
171160
}
172161

173-
// GetRepository return a known repository
174-
func (m *Manager) GetRepository(repoName string) (*Repository, error) {
162+
// getRepository returns a repository from the cache
163+
func (m *Manager) getRepository(repoName string) (*Repository, error) {
175164
repo, ok := m.repoCache[repoName]
176165
if !ok {
177166
return nil, ErrRepositoryNotCached
@@ -185,7 +174,7 @@ func (m *Manager) List(filters ...Filter) []*Repository {
185174
repoNames := append(m.cfg.Repositories.Core, m.cfg.Repositories.Services...)
186175
mainLoop:
187176
for _, repoName := range repoNames {
188-
repo, err := m.GetRepository(repoName)
177+
repo, err := m.LoadRepository(repoName)
189178
if err != nil {
190179
continue
191180
}
@@ -201,19 +190,19 @@ mainLoop:
201190

202191
// Clone clones a known repository to the config root directory
203192
func (m *Manager) clone(ctx context.Context, repoName string) error {
204-
repo, err := m.GetRepository(repoName)
193+
repo, err := m.LoadRepository(repoName)
205194
if err != nil {
206195
return fmt.Errorf("cannot clone repository %s: %v", repoName, err)
207196
}
208197
if repo.gitRepo != nil {
209198
return ErrRepositoryAlreadyExist
210199
}
211200

212-
// clone fork repository
201+
// clone fork repository with original name
213202
err = m.git.Clone(
214203
ctx,
215204
m.urlBuilder(m.cfg.Github.Username, repo.ExpectedForkName),
216-
repo.ExpectedForkName,
205+
repo.FullPath,
217206
)
218207
if errors.Is(err, transport.ErrAuthenticationRequired) {
219208
return ErrUnauthenticated
@@ -276,37 +265,20 @@ func (m *Manager) EnsureFork(ctx context.Context, repo *Repository) error {
276265
return err
277266
}
278267

279-
// used to help mocking os.Rename
280-
// TODO(hilalymh): Q4 switch to go1.16 os/fs library/interface
281-
var renameDirectory = os.Rename
282-
283268
func (m *Manager) EnsureClone(ctx context.Context, repo *Repository) error {
284269
err := m.clone(ctx, repo.Name)
285270
if err != nil && err != ErrRepositoryAlreadyExist {
286271
return err
287272
}
288273

289-
// At this point we ensured that the fork repository is cloned. We need to rename it
290-
// if there is any fork prefix.
291-
if repo.Name != repo.ExpectedForkName {
292-
293-
newPath := filepath.Join(
294-
filepath.Dir(repo.FullPath),
295-
repo.Name,
296-
)
297-
err := renameDirectory(repo.FullPath, newPath)
298-
if err != nil {
299-
return err
300-
}
301-
}
302-
303274
return nil
304275
}
305276

306-
// EnsureAll ensures one repository.
277+
// EnsureRepository ensures the current user owns a fork of the given repository
278+
// and has cloned it.
307279
func (m *Manager) EnsureRepository(ctx context.Context, name string) error {
308-
repo, err := m.GetRepository(name)
309-
if err != nil {
280+
repo, err := m.LoadRepository(name)
281+
if err != nil && err != ErrRepositoryDoesntExist {
310282
return err
311283
}
312284

pkg/repository/repository.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import (
2121

2222
// NewRepository returns a pointer to a new repository.
2323
func NewRepository(name string, repoType RepositoryType) *Repository {
24-
if repoType == RepositoryTypeController {
25-
name = fmt.Sprintf("%s-controller", name)
26-
}
2724
return &Repository{
2825
Name: name,
2926
Type: repoType,

0 commit comments

Comments
 (0)