The problem (again)
+----------+ +----------+
| cli opts | -> | project | -+->
| (done) | | config | |
+----------+ +----------+ |
|
+->
|
|
|
+->
Interested in go vs typescript
If you are curious Go - TS. With rust included! TS - Go - Rust.
Lets build the Projector object
src/projector/config.go
LETS BUILD!!!
The code!
package projector
import (
"os"
"path"
)
type Operation = int
const (
Print Operation = iota
Add
Remove
)
type ProjectorConfig struct {
Pwd string // projector --pwd ...
Config string // projector --config ...
Operation Operation // print, add, remove
Arguments []string // <key>*, <key> <value>, <key>
}
func getConfig(config string) (string, error) {
if config == "" {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return path.Join(configDir, "projector", "projector.json"), nil
}
return config, nil
}
func getPwd(pwd string) (string, error) {
if pwd == "" {
wd, err := os.Getwd()
return wd, err
}
return pwd, nil
}
func isOperationCommand(op string) bool {
return op == "add" || op == "rm";
}
func getArguments(commands []string) []string {
if (len(commands) > 0 && isOperationCommand(commands[0])) {
return commands[1:]
}
return commands
}
func getOperation(commands []string) Operation {
if len(commands) == 0 {
return Add
}
switch (commands[0]) {
case "add": return Add
case "rm": return Remove
}
return Print
}
func NewProjectorConfig(opts *ProjectorOpts) (*ProjectorConfig, error) {
pwd, err := getPwd(opts.Pwd)
if err != nil {
return nil, err
}
config, err := getConfig(opts.Config)
if err != nil {
return nil, err
}
return &ProjectorConfig {
Operation: getOperation(opts.Arguments),
Arguments: getArguments(opts.Arguments),
Pwd: pwd,
Config: config,
}, nil
}
Onto the Lords language
no i am not talking about Php