Skip to content

Commit 2f8852f

Browse files
committed
devshell: split into multiple packages
Make room for more extensions
1 parent 4859a38 commit 2f8852f

File tree

9 files changed

+83
-53
lines changed

9 files changed

+83
-53
lines changed

devshell/cmd/cmd.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
// Execute the main command
10+
func Execute() error {
11+
app := &cli.App{
12+
Name: "devshell",
13+
Description: "THE developer shell",
14+
Commands: []*cli.Command{
15+
Enter,
16+
Init,
17+
},
18+
}
19+
20+
return app.Run(os.Args)
21+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"fmt"
@@ -17,7 +17,8 @@ in
1717
pkgs.mkDevShell.fromTOML ./devshell.toml
1818
`
1919

20-
var cmdEnter = &cli.Command{
20+
// Enter command
21+
var Enter = &cli.Command{
2122
Name: "enter",
2223
Aliases: []string{"e"},
2324
Usage: "builds and enters the shell",
Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
package main
1+
package cmd
22

33
import (
44
"os"
55
"path/filepath"
66

7+
"github.com/numtide/devshell/devshell/config"
8+
79
"github.com/urfave/cli/v2"
810
)
911

10-
const initHeader = `
11-
# See https://github.com/numtide/devshell
12-
`
13-
14-
var cmdInit = &cli.Command{
12+
// Init command
13+
var Init = &cli.Command{
1514
Name: "init",
16-
Usage: "creates a new " + configFile + " file",
15+
Usage: "creates a new " + config.FileName + " file",
1716
Flags: []cli.Flag{
1817
&cli.StringFlag{
1918
Name: "name",
@@ -37,21 +36,6 @@ var cmdInit = &cli.Command{
3736
name = filepath.Base(p2)
3837
}
3938

40-
// Generate the config
41-
cfg := &config{
42-
Name: name,
43-
}
44-
cfgStr := initHeader + configPrint(cfg)
45-
46-
// Write the config file
47-
w, err := os.Create(filepath.Join(p, configFile))
48-
if err != nil {
49-
return err
50-
}
51-
_, err = w.WriteString(cfgStr)
52-
if err != nil {
53-
return err
54-
}
55-
return w.Close()
39+
return config.Init(p, name)
5640
},
5741
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"os"
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
1-
package main
1+
package config
22

33
import (
44
"os"
55

66
"github.com/pelletier/go-toml"
77
)
88

9-
const configFile = "devshell.toml"
9+
// FileName ...
10+
const FileName = "devshell.toml"
1011

11-
type configBash struct {
12+
// Bash ...
13+
type Bash struct {
1214
Extra string `toml:"extra,omitempty"`
1315
Interactive string `toml:"interactive,omitempty"`
1416
}
1517

16-
type configCommand struct {
18+
// Command ...
19+
type Command struct {
1720
Alias string `toml:"alias,omitempty"`
1821
Command string `toml:"command,omitempty"`
1922
Help string `toml:"help,omitempty"`
2023
Name string `toml:"name"`
2124
Package string `toml:"package,omitempty"`
2225
}
2326

24-
type config struct {
27+
// Config ...
28+
type Config struct {
2529
Name string `toml:"name"`
2630
Packages []string `toml:"packages"`
2731
Motd *string `toml:"motd"`
2832
Env map[string]interface{} `toml:"env"`
29-
Bash configBash `toml:"bash,omitempty"`
30-
Commands []configCommand `toml:"commands"`
33+
Bash Bash `toml:"bash,omitempty"`
34+
Commands []Command `toml:"commands"`
3135
}
3236

33-
func configLoad(path string) (*config, error) {
37+
// Load ...
38+
func Load(path string) (*Config, error) {
3439
r, err := os.Open(path)
3540
if err != nil {
3641
return nil, err
3742
}
3843

39-
c := &config{}
44+
c := &Config{}
4045
err = toml.NewDecoder(r).Decode(c)
4146
if err != nil {
4247
return nil, err
@@ -45,7 +50,8 @@ func configLoad(path string) (*config, error) {
4550
return c, err
4651
}
4752

48-
func configPrint(c *config) string {
53+
// Print ...
54+
func Print(c *Config) string {
4955
b, err := toml.Marshal(c)
5056
if err != nil {
5157
panic(err) // should never happen

devshell/config/init.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
)
7+
8+
const initHeader = `
9+
# See https://github.com/numtide/devshell
10+
`
11+
12+
// Init ...
13+
func Init(path string, name string) error {
14+
// Generate the config
15+
cfg := &Config{
16+
Name: name,
17+
}
18+
cfgStr := initHeader + Print(cfg)
19+
20+
// Write the config file
21+
w, err := os.Create(filepath.Join(path, FileName))
22+
if err != nil {
23+
return err
24+
}
25+
_, err = w.WriteString(cfgStr)
26+
if err != nil {
27+
return err
28+
}
29+
return w.Close()
30+
}

devshell/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/numtide/devshell/go/devshell
1+
module github.com/numtide/devshell/devshell
22

33
go 1.15
44

devshell/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
22
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
33
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/numtide/devshell v0.0.0-20201009170333-4859a3846246 h1:B69NqAcy4dHvDgtUVrzSe0kZxp4g2JtfdmxKfqwNMBU=
6+
github.com/numtide/devshell/devshell v0.0.0-20201009170333-4859a3846246 h1:k4CTKrya/18Pl0FE2cJFLTRIN0zazmSA4LTi8dMukPs=
57
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
68
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
79
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

devshell/main.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@ package main
22

33
import (
44
"log"
5-
"os"
65

7-
"github.com/urfave/cli/v2"
6+
"github.com/numtide/devshell/devshell/cmd"
87
)
98

10-
func run() error {
11-
app := &cli.App{
12-
Name: "devshell",
13-
Description: "THE developer shell",
14-
Commands: []*cli.Command{
15-
cmdEnter,
16-
cmdInit,
17-
},
18-
}
19-
20-
return app.Run(os.Args)
21-
}
22-
239
func main() {
24-
err := run()
10+
err := cmd.Execute()
2511
if err != nil {
2612
log.Fatal(err)
2713
}

0 commit comments

Comments
 (0)