Skip to content

Commit 5f71f33

Browse files
committedSep 17, 2020
feat: Restructure the CMD package
1 parent 092a214 commit 5f71f33

File tree

13 files changed

+167
-144
lines changed

13 files changed

+167
-144
lines changed
 

‎cmd/install.go

-25
This file was deleted.

‎cmd/install/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package install
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// Register registers install command.
12+
func Register(root *cobra.Command) {
13+
cmd := &cobra.Command{
14+
Use: "install [name of packages]",
15+
Aliases: []string{"i"},
16+
Short: "installs a package",
17+
Long: "Installing package(s) in DonyaOS",
18+
Args: cobra.MinimumNArgs(1),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
leng := strconv.Itoa(len(args))
21+
fmt.Println("Installing " + leng + " package(s): " + strings.Join(args, " "))
22+
},
23+
}
24+
25+
root.AddCommand(cmd)
26+
}

‎cmd/purge.go

-25
This file was deleted.

‎cmd/purge/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package purge
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// Register registers purge command.
12+
func Register(root *cobra.Command) {
13+
cmd := &cobra.Command{
14+
Use: "purge [name of packages]",
15+
Aliases: []string{"p"},
16+
Short: "removes all orphaned packages",
17+
Long: "Removing all orphaned package(s) in DonyaOS",
18+
Args: cobra.MinimumNArgs(1),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
leng := strconv.Itoa(len(args))
21+
fmt.Println("Purging " + leng + " package(s): " + strings.Join(args, " "))
22+
},
23+
}
24+
25+
root.AddCommand(cmd)
26+
}

‎cmd/root.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/DonyaOS/PackageManager/cmd/install"
8+
"github.com/DonyaOS/PackageManager/cmd/purge"
9+
"github.com/DonyaOS/PackageManager/cmd/search"
710
"github.com/spf13/cobra"
811
)
912

10-
var cfgFile string
11-
12-
// rootCmd represents the base command when called without any subcommands
13-
var rootCmd = &cobra.Command{
14-
Use: "todo",
15-
Short: "A simple todo list made with golan",
16-
}
13+
// ExitFailure status code.
14+
const ExitFailure = 1
1715

1816
// Execute adds all child commands to the root command and sets flags appropriately.
1917
// This is called by main.main(). It only needs to happen once to the rootCmd.
2018
func Execute() {
21-
if err := rootCmd.Execute(); err != nil {
22-
fmt.Println(err)
23-
os.Exit(1)
19+
root := &cobra.Command{
20+
Use: "donya",
21+
Short: "Donya Package Manager/System",
22+
}
23+
24+
install.Register(root)
25+
purge.Register(root)
26+
search.Register(root)
27+
28+
if err := root.Execute(); err != nil {
29+
fmt.Printf("error: %s\n", err.Error())
30+
os.Exit(ExitFailure)
2431
}
2532
}

‎cmd/search.go

-25
This file was deleted.

‎cmd/search/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package search
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// Register registers search command.
12+
func Register(root *cobra.Command) {
13+
cmd := &cobra.Command{
14+
Use: "search [name of packages]",
15+
Aliases: []string{"s"},
16+
Short: "searches for a package",
17+
Long: "Searching for a package(s) in DonyaOS",
18+
Args: cobra.MinimumNArgs(1),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
leng := strconv.Itoa(len(args))
21+
fmt.Println("Searching for " + leng + " package(s): " + strings.Join(args, " "))
22+
},
23+
}
24+
25+
root.AddCommand(cmd)
26+
}

‎cmd/uninstall.go

-25
This file was deleted.

‎cmd/uninstall/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// Register registers uninstall command.
12+
func Register(root *cobra.Command) {
13+
cmd := &cobra.Command{
14+
Use: "uninstall [name of packages]",
15+
Aliases: []string{"r", "remove"},
16+
Short: "removes a package",
17+
Long: "Removing package(s) in DonyaOS",
18+
Args: cobra.MinimumNArgs(1),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
leng := strconv.Itoa(len(args))
21+
fmt.Println("Uninstalling " + leng + " package(s): " + strings.Join(args, " "))
22+
},
23+
}
24+
25+
root.AddCommand(cmd)
26+
}

‎cmd/update.go

-25
This file was deleted.

‎cmd/update/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// Register registers update command.
12+
func Register(root *cobra.Command) {
13+
cmd := &cobra.Command{
14+
Use: "update [name of packages]",
15+
Aliases: []string{"u"},
16+
Short: "updates the list of packages",
17+
Long: "Updating the list of packages in DonyaOS",
18+
Args: cobra.MinimumNArgs(1),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
leng := strconv.Itoa(len(args))
21+
fmt.Println("Updating " + leng + " package(s): " + strings.Join(args, " "))
22+
},
23+
}
24+
25+
root.AddCommand(cmd)
26+
}

‎donya.go

-9
This file was deleted.

‎main.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/DonyaOS/PackageManager/cmd"
7+
)
8+
9+
// nolint: gocheckglobals
10+
var (
11+
version = "dev"
12+
commit = "none"
13+
date = "unknown"
14+
)
15+
16+
func main() {
17+
fmt.Printf("gosimac %s, commit %s, built at %s\n", version, commit, date)
18+
19+
cmd.Execute()
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.