Skip to content

Commit a80d38a

Browse files
committed
feat: add dtm patch subcommand
Signed-off-by: Daniel Hu <[email protected]>
1 parent fdf597f commit a80d38a

File tree

18 files changed

+607
-27
lines changed

18 files changed

+607
-27
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ endif
1919
FIND := find . -path './cmd/*.go' -o -path './internal/pkg/*.go'
2020

2121
.PHONY: build
22-
build: fmt lint vet mod-tidy ## Build dtm core only, without plugins, locally.
22+
build: fmt vet mod-tidy ## Build dtm core only, without plugins, locally.
2323
go build -trimpath -o dtm .
2424
@echo "${GREEN}✔'dtm' has been generated in the current directory($(PWD))!${RESET}"
2525

@@ -30,10 +30,10 @@ fmt: verify.goimports ## Run 'go fmt' & goimports against code.
3030
@$(FIND) -type f | xargs ${GOPATH}/bin/goimports -w -local $(DTM_ROOT)
3131
@go mod edit -fmt
3232

33-
.PHONY: lint
34-
lint: verify.golangcilint ## Run 'golangci-lint' against code.
35-
@echo "$(YELLOW)Run golangci to lint source codes$(RESET)"
36-
@${GOPATH}/bin/golangci-lint -c $(ROOT_DIR)/.golangci.yml run $(ROOT_DIR)/...
33+
#.PHONY: lint
34+
#lint: verify.golangcilint ## Run 'golangci-lint' against code.
35+
# @echo "$(YELLOW)Run golangci to lint source codes$(RESET)"
36+
# @${GOPATH}/bin/golangci-lint -c $(ROOT_DIR)/.golangci.yml run $(ROOT_DIR)/...
3737

3838
.PHONY: vet
3939
vet: ## Run "go vet ./...".

cmd/github.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
var githubCmd = &cobra.Command{
1111
Use: "github",
1212
Short: "github is used to execute github operations",
13-
Long: `github is used to execute github operations`,
13+
Long: `github is used to execute github operations
14+
参考 gh`,
1415
Run: func(cmd *cobra.Command, args []string) {
1516
github.Run()
1617
},

cmd/patch.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"os"
8+
9+
"github.com/devstream-io/devstream/internal/response"
10+
11+
"github.com/devstream-io/devstream/internal/log"
12+
"github.com/devstream-io/devstream/internal/pkg/patch"
13+
14+
"github.com/spf13/cobra"
15+
)
16+
17+
// patchCmd represents the patch command
18+
var patchCmd = &cobra.Command{
19+
Use: "patch",
20+
Short: "apply a diff file to an original",
21+
Long: `patch will take a patch file containing any of the four forms of difference listing
22+
produced by the diff program and apply those differences to an original file,
23+
producing a patched version. If patchfile is omitted, or is a hyphen,
24+
the patch will be read from the standard input.
25+
26+
e.g.
27+
- dtm patch file.patch
28+
- dtm patch file.patch -ojson
29+
`,
30+
Run: func(cmd *cobra.Command, args []string) {
31+
if len(args) != 1 {
32+
log.Error("Incorrect number of arguments")
33+
os.Exit(1)
34+
}
35+
err := patch.Patch(".", args[0])
36+
if err != nil {
37+
log.Error(err)
38+
r := response.New(response.StatusError, response.MessageError, err.Error())
39+
r.Print(OutputFormat)
40+
os.Exit(1)
41+
}
42+
r := response.New(response.StatusError, response.MessageOK, "")
43+
r.Print(OutputFormat)
44+
},
45+
}
46+
47+
func init() {
48+
rootCmd.AddCommand(patchCmd)
49+
}

cmd/root.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,33 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/sirupsen/logrus"
8+
9+
"github.com/devstream-io/devstream/internal/log"
10+
711
"github.com/spf13/cobra"
812
"github.com/spf13/viper"
13+
14+
"github.com/devstream-io/devstream/internal/option"
915
)
1016

1117
var cfgFile string
18+
19+
// isDebug is a flag to enable debug level log
20+
var isDebug bool
21+
22+
// OutputFormat is the output format for the command. One of: json|yaml|raw
23+
// Default value is "raw"
1224
var OutputFormat string
1325

1426
// rootCmd represents the base command when called without any subcommands
1527
var rootCmd = &cobra.Command{
1628
Use: "dtm",
1729
Short: "dtm is a tool to manage variaties of development platforms",
1830
Long: `dtm is a tool to manage variaties of development platforms.`,
31+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
32+
initLog()
33+
},
1934
}
2035

2136
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -30,14 +45,9 @@ func Execute() {
3045
func init() {
3146
cobra.OnInitialize(initConfig)
3247

33-
// Here you will define your flags and configuration settings.
34-
// Cobra supports persistent flags, which, if defined here,
35-
// will be global for your application.
36-
3748
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.devstream.yaml)")
38-
39-
// Cobra also supports local flags, which will only run
40-
// when this action is called directly.
49+
rootCmd.PersistentFlags().StringVarP(&OutputFormat, "output", "o", "raw", "Output format. One of: json|yaml|raw")
50+
rootCmd.PersistentFlags().BoolVarP(&isDebug, "debug", "", false, "debug level log")
4151
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4252
}
4353

@@ -63,4 +73,20 @@ func initConfig() {
6373
if err := viper.ReadInConfig(); err == nil {
6474
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
6575
}
76+
77+
if OutputFormat != "raw" {
78+
option.Silence = true
79+
}
80+
}
81+
82+
func initLog() {
83+
// if OutputFormat is not "raw", set log level to PanicLevel to disable log
84+
if OutputFormat != "raw" {
85+
logrus.SetLevel(logrus.PanicLevel)
86+
} else if isDebug {
87+
logrus.SetLevel(logrus.DebugLevel)
88+
log.Infof("Log level is: %s.", logrus.GetLevel())
89+
} else {
90+
logrus.SetLevel(logrus.InfoLevel)
91+
}
6692
}

cmd/scaffold.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"os"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/devstream-io/devstream/internal/log"
12+
"github.com/devstream-io/devstream/internal/pkg/scaffold"
13+
)
14+
15+
var structure string
16+
17+
// scaffoldCmd represents the scaffold command
18+
var scaffoldCmd = &cobra.Command{
19+
Use: "scaffold",
20+
Short: "scaffold is used to generate folder and file structure",
21+
Long: `
22+
dtm scaffold "
23+
project/
24+
├── src/
25+
│ ├── main.go
26+
│ └── utils/
27+
│ ├── file1.go
28+
│ └── file2.go
29+
└── README.md
30+
"
31+
`,
32+
Run: func(cmd *cobra.Command, args []string) {
33+
if len(args) != 1 {
34+
log.Error("Incorrect number of arguments")
35+
os.Exit(1)
36+
}
37+
if err := scaffold.Scaffold(args[0]); err != nil {
38+
log.Error(err)
39+
os.Exit(1)
40+
}
41+
},
42+
}
43+
44+
func init() {
45+
rootCmd.AddCommand(scaffoldCmd)
46+
47+
scaffoldCmd.Flags().StringVarP(&structure, "structure", "s", "", "structure specify the folder and file structure")
48+
}

go.mod

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@ module github.com/devstream-io/devstream
33
go 1.20
44

55
require (
6+
github.com/onsi/ginkgo v1.16.5
7+
github.com/onsi/gomega v1.27.6
8+
github.com/sirupsen/logrus v1.9.0
69
github.com/spf13/cobra v1.7.0
710
github.com/spf13/viper v1.15.0
11+
gopkg.in/gookit/color.v1 v1.1.6
12+
gopkg.in/yaml.v3 v3.0.1
813
)
914

1015
require (
16+
github.com/frankban/quicktest v1.14.4 // indirect
1117
github.com/fsnotify/fsnotify v1.6.0 // indirect
18+
github.com/google/go-cmp v0.5.9 // indirect
1219
github.com/hashicorp/hcl v1.0.0 // indirect
1320
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1421
github.com/magiconair/properties v1.8.7 // indirect
1522
github.com/mitchellh/mapstructure v1.5.0 // indirect
23+
github.com/nxadm/tail v1.4.8 // indirect
1624
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
17-
github.com/sirupsen/logrus v1.9.0 // indirect
1825
github.com/spf13/afero v1.9.3 // indirect
1926
github.com/spf13/cast v1.5.0 // indirect
2027
github.com/spf13/jwalterweatherman v1.1.0 // indirect
2128
github.com/spf13/pflag v1.0.5 // indirect
2229
github.com/subosito/gotenv v1.4.2 // indirect
23-
golang.org/x/sys v0.3.0 // indirect
24-
golang.org/x/text v0.5.0 // indirect
25-
gopkg.in/gookit/color.v1 v1.1.6 // indirect
30+
golang.org/x/net v0.8.0 // indirect
31+
golang.org/x/sys v0.7.0 // indirect
32+
golang.org/x/text v0.8.0 // indirect
2633
gopkg.in/ini.v1 v1.67.0 // indirect
27-
gopkg.in/yaml.v3 v3.0.1 // indirect
34+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
2835
)

0 commit comments

Comments
 (0)