Skip to content

Commit 74eec15

Browse files
authored
chore: Create cli package (#157)
1 parent 2cef2fa commit 74eec15

File tree

8 files changed

+70
-27
lines changed

8 files changed

+70
-27
lines changed

cmd/init.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
package cmd
22

33
import (
4-
_ "embed"
5-
"fmt"
4+
"os"
65

7-
"github.com/koki-develop/clive/internal/util"
6+
"github.com/koki-develop/clive/internal/cli"
87
"github.com/spf13/cobra"
98
)
109

11-
//go:embed clive.yml
12-
var configInitTemplate []byte
13-
1410
var initCmd = &cobra.Command{
1511
Use: "init",
1612
Short: "Create a config file",
1713
Long: "Create a config file.",
1814
Args: cobra.ExactArgs(0),
1915
RunE: func(cmd *cobra.Command, args []string) error {
20-
exists, err := util.Exists(flagConfig)
21-
if err != nil {
22-
return err
23-
}
24-
if exists {
25-
return fmt.Errorf("%s already exists", flagConfig)
26-
}
27-
28-
f, err := util.CreateFile(flagConfig)
29-
if err != nil {
30-
return err
31-
}
32-
defer f.Close()
33-
34-
if _, err := f.Write(configInitTemplate); err != nil {
35-
return err
36-
}
16+
c := cli.New(&cli.Config{
17+
Stdout: os.Stdout,
18+
})
3719

38-
_, _ = fmt.Printf("Created %s\n", flagConfig)
39-
return nil
20+
return c.Init(&cli.InitParams{
21+
Config: flagConfig,
22+
})
4023
},
4124
}

cmd/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9+
// TODO: move to internal/cli
910
var startCmd = &cobra.Command{
1011
Use: "start",
1112
Short: "Start cLive actions",

cmd/validate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10+
// TODO: move to internal/cli
1011
var validateCmd = &cobra.Command{
1112
Use: "validate",
1213
Short: "Validate a config file",

internal/cache/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewStore(ttl time.Duration) (*Store, error) {
3030

3131
func (s *Store) Get(key string) (*Cache, error) {
3232
p := s.buildPath(key)
33-
exists, err := util.Exists(p)
33+
exists, err := util.FileExists(p)
3434
if err != nil {
3535
return nil, err
3636
}

internal/cli/cli.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cli
2+
3+
import "io"
4+
5+
type CLI struct {
6+
stdout io.Writer
7+
}
8+
9+
type Config struct {
10+
Stdout io.Writer
11+
}
12+
13+
func New(cfg *Config) *CLI {
14+
return &CLI{
15+
stdout: cfg.Stdout,
16+
}
17+
}

internal/cli/init.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cli
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
7+
"github.com/koki-develop/clive/internal/util"
8+
)
9+
10+
//go:embed static/clive.yml
11+
var configInitTemplate []byte
12+
13+
type InitParams struct {
14+
Config string
15+
}
16+
17+
func (c *CLI) Init(p *InitParams) error {
18+
exists, err := util.FileExists(p.Config)
19+
if err != nil {
20+
return err
21+
}
22+
if exists {
23+
return fmt.Errorf("%s already exists", p.Config)
24+
}
25+
26+
f, err := util.CreateFile(p.Config)
27+
if err != nil {
28+
return err
29+
}
30+
defer f.Close()
31+
32+
if _, err := f.Write(configInitTemplate); err != nil {
33+
return err
34+
}
35+
36+
if _, err = fmt.Fprintf(c.stdout, "Created %s\n", p.Config); err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}

internal/util/files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func CreateFile(p string) (*os.File, error) {
1919
return f, nil
2020
}
2121

22-
func Exists(p string) (bool, error) {
22+
func FileExists(p string) (bool, error) {
2323
if _, err := os.Stat(p); err != nil {
2424
if os.IsNotExist(err) {
2525
return false, nil

0 commit comments

Comments
 (0)