Skip to content

Commit a5d0cdd

Browse files
committed
feat: clone command
1 parent 6da62cc commit a5d0cdd

File tree

7 files changed

+200
-2
lines changed

7 files changed

+200
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log (go-package-manager)
22

3+
## 0.44.0
4+
5+
- feat: add `clone` command
6+
- fix: `password` command
7+
- tests: add more tests
8+
39
## 0.43.5
410

511
- ci: implement first tests

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- [Branch aliases](#branch-aliases-)
3535
- [Checkup project](#checkup-project-)
3636
- [Cleanup project](#cleanup-project-)
37+
- [Clone project](#clone-project-)
3738
- [Compare code changes](#compare-code-changes-)
3839
- [Compression](#compression-)
3940
- [Cron jobs](#cron-jobs-)
@@ -64,6 +65,7 @@
6465
- [Run script](#run-script-)
6566
- [Run tests](#run-tests-)
6667
- [Show dependency graph](#show-dependency-graph-)
68+
- [Sleep](#sleep-)
6769
- [Start project](#start-project-)
6870
- [Synchronize with Git remotes](#synchronize-with-git-remotes-)
6971
- [Uninstall dependencies](#uninstall-dependencies-)
@@ -281,6 +283,28 @@ gpm tidy
281283

282284
is a short form of `go mod tidy`.
283285

286+
#### Clone project [<a href="#commands-">↑</a>]
287+
288+
This command allows you to clone a [known project](#add-project-) using a shorter alias.
289+
290+
For example, if you’ve added a project named `hugo` with
291+
292+
```bash
293+
gpm add project https://github.com/gohugoio/hugo
294+
```
295+
296+
you can later clone it with a simple
297+
298+
```bash
299+
gpm clone hugo
300+
```
301+
302+
which is a shorthand for `git clone https://github.com/gohugoio/hugo`, what means that you are able to add any [clone argument](https://git-scm.com/docs/git-clone) with your installed Git command version:
303+
304+
```bash
305+
gpm clone hugo -o my-projects/the-great-hugo-project
306+
```
307+
284308
#### Compare code changes [<a href="#commands-">↑</a>]
285309

286310
![Diff demo 1](./img/demos/diff-demo-1.gif)

app/new.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func New() (*types.AppContext, *cobra.Command, error) {
107107
commands.Init_Cat_Command(rootCmd, app)
108108
commands.Init_Chat_Command(rootCmd, app)
109109
commands.Init_Checkout_Command(rootCmd, app)
110+
commands.Init_Clone_Command(rootCmd, app)
110111
commands.Init_Compress_Command(rootCmd, app)
111112
commands.Init_Cron_Command(rootCmd, app)
112113
commands.Init_Describe_Command(rootCmd, app)

commands/clone.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
package commands
24+
25+
import (
26+
"strings"
27+
28+
"github.com/mkloubert/go-package-manager/types"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
func Init_Clone_Command(parentCmd *cobra.Command, app *types.AppContext) {
33+
var cloneCmd = &cobra.Command{
34+
Use: "clone",
35+
Short: "Clone project",
36+
Long: `Clones a project by using its alias.`,
37+
Args: cobra.MinimumNArgs(1),
38+
Run: func(cmd *cobra.Command, args []string) {
39+
projectName := strings.TrimSpace(args[0])
40+
41+
allGitArgs := make([]string, 0)
42+
allGitArgs = append(allGitArgs, "clone")
43+
44+
projectUrl, ok := app.ProjectsFile.Projects[projectName]
45+
if ok {
46+
allGitArgs = append(allGitArgs, projectUrl)
47+
} else {
48+
allGitArgs = append(allGitArgs, projectName)
49+
}
50+
51+
allGitArgs = append(allGitArgs, args[1:]...)
52+
53+
app.RunShellCommandByArgs("git", allGitArgs...)
54+
},
55+
}
56+
57+
cloneCmd.DisableFlagParsing = true
58+
59+
parentCmd.AddCommand(
60+
cloneCmd,
61+
)
62+
}

commands/password.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Init_Password_Command(parentCmd *cobra.Command, app *types.AppContext) {
5656

5757
i++
5858
if i > 1 {
59-
fmt.Println()
59+
app.WriteString(fmt.Sprintln())
6060
addClipboardContent(fmt.Sprintln())
6161

6262
time.Sleep(time.Duration(waitTime) * time.Millisecond)
@@ -109,7 +109,7 @@ func Init_Password_Command(parentCmd *cobra.Command, app *types.AppContext) {
109109
}
110110

111111
if !noOutput {
112-
fmt.Print(passwordToOutput)
112+
app.WriteString(passwordToOutput)
113113
}
114114

115115
addClipboardContent(passwordToOutput)

tests/commands/password_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
package tests
24+
25+
import (
26+
"testing"
27+
28+
tests "github.com/mkloubert/go-package-manager/tests"
29+
)
30+
31+
func TestPasswordCommand(t *testing.T) {
32+
tests.WithApp(t, func(ctx *tests.WithAppActionContext) error {
33+
ctx.SetArgs("password")
34+
35+
if ctx.Execute() {
36+
output := ctx.Output.String()
37+
38+
ctx.ExpectValue(len(output), 20, "")
39+
}
40+
41+
return nil
42+
})
43+
}
44+
45+
func TestPasswordWithLengthCommand(t *testing.T) {
46+
tests.WithApp(t, func(ctx *tests.WithAppActionContext) error {
47+
ctx.SetArgs("password", "--length", "67")
48+
49+
if ctx.Execute() {
50+
output := ctx.Output.String()
51+
52+
ctx.ExpectValue(len(output), 67, "")
53+
}
54+
55+
return nil
56+
})
57+
}

tests/commands/sleep_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
package tests
24+
25+
import (
26+
"testing"
27+
"time"
28+
29+
tests "github.com/mkloubert/go-package-manager/tests"
30+
)
31+
32+
func TestSleepCommand(t *testing.T) {
33+
tests.WithApp(t, func(ctx *tests.WithAppActionContext) error {
34+
ctx.SetArgs("sleep", "1s")
35+
36+
start := time.Now()
37+
if ctx.Execute() {
38+
end := time.Now()
39+
40+
duration := end.Sub(start)
41+
ms := duration.Milliseconds()
42+
43+
ctx.ExpectTrue(ms >= 1000, "Expected to sleep at wait 1000 milliseconds")
44+
}
45+
46+
return nil
47+
})
48+
}

0 commit comments

Comments
 (0)