Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to pin to a commit using the add command #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (

func NewAddCommand() *cobra.Command {
cmds := &cobra.Command{
Use: "add [DST_PATH] [DST_FOLDER_NAME] [REPO_URL] [REPO_REF] [REPO_FOLDER]",
Use: "add dst_path dst_folder_name repo_url repo_path repo_ref [repo_hash]",
Short: "Add a new target to sync from an upstream git repository",
Example: `Sync the 'logo' directory from the main branch of the cert-manager
community repository to the local directory ./a/b

klone add a b https://github.com/cert-manager/community.git main logo`,
Args: cobra.ExactArgs(5),
klone add a b https://github.com/cert-manager/community.git logo main
or with pinned commit hash:
klone add a b https://github.com/cert-manager/community.git logo main 9f0ea0341816665feadcdcfb7744f4245604ab28`,
Args: cobra.RangeArgs(5, 6),
RunE: func(cmd *cobra.Command, args []string) error {
workDirPath, err := filepath.Abs(".")
if err != nil {
Expand All @@ -28,13 +30,19 @@ klone add a b https://github.com/cert-manager/community.git main logo`,
dstPath := args[0]
dstFolderName := args[1]
repoURL := args[2]
ref := args[3]
srcFolder := args[4]
repoPath := args[3]
repoRef := args[4]

repoHash := ""
if len(args) == 6 {
repoHash = args[5]
}

return wrkDir.AddTarget(dstPath, dstFolderName, mod.KloneSource{
RepoURL: repoURL,
RepoRef: ref,
RepoPath: srcFolder,
RepoPath: repoPath,
RepoRef: repoRef,
RepoHash: repoHash,
})
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ which does nothing.

To add a target for kloning, use "klone add", e.g.:

klone add example myfolder https://github.com/cert-manager/community.git main logo
klone add example myfolder https://github.com/cert-manager/community.git logo main

This will add an entry to klone.yaml which fetches the latest cert-manager
logo from the community repo and stores it in example/myfolder.
Expand Down
26 changes: 2 additions & 24 deletions pkg/mod/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,6 @@ func (w WorkDir) editKloneFile(fn func(*kloneFile) error) error {
return nil
}

func (w WorkDir) readKloneFile() (*kloneFile, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not used.

kloneFilePath := filepath.Join(string(w), kloneFileName)

// exclusively open or create index file
file, err := lockedfile.Open(kloneFilePath)
if err != nil {
return nil, err
}
defer file.Close()

index := kloneFile{}

// decode current contents of index file
if err := yaml.NewDecoder(file).Decode(&index); err != nil && err != io.EOF {
return nil, err
}

// canonicalize index
index.canonicalize()

return &index, nil
}

func (w WorkDir) Init() error {
return w.editKloneFile(func(kf *kloneFile) error {
return nil
Expand All @@ -185,9 +162,10 @@ func (w WorkDir) Init() error {

func (w WorkDir) AddTarget(target string, folderName string, dep KloneSource) error {
return w.editKloneFile(func(kf *kloneFile) error {
for _, src := range kf.Targets[target] {
for targetFolder, src := range kf.Targets[target] {
if src.FolderName == folderName {
src.KloneSource = dep
kf.Targets[target][targetFolder] = src
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error was found by my ide linter.

return nil
}
}
Expand Down
10 changes: 9 additions & 1 deletion test-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ pushd "$temp_dir"
mkdir -p a/b
touch a/SHOULD_NOT_BE_DELETED
touch a/b/SHOULD_BE_DELETED
"$klone_binary" add a/b c/d https://github.com/cert-manager/community.git main logo
"$klone_binary" add a/b c/d https://github.com/cert-manager/community.git logo main
"$klone_binary" add a/b e https://github.com/cert-manager/community.git logo main 9f0ea0341816665feadcdcfb7744f4245604ab28
"$klone_binary" sync
if [ -f a/SHOULD_NOT_BE_DELETED ] && [ ! -f a/b/SHOULD_BE_DELETED ]; then
echo "Test passed"
else
echo "Test failed"
exit 1
fi
cat klone.yaml
tree -a
popd
Loading