-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Congqi Xia <[email protected]>
- Loading branch information
Showing
63 changed files
with
2,413 additions
and
1,506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package configs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const ( | ||
configFileName = `birdwatcher.yaml` | ||
defaultWorkspace = `bw_workspace` | ||
) | ||
|
||
var ( | ||
errConfigPathNotExist = errors.New("config path not exist") | ||
errConfigPathIsFile = errors.New("config path is file") | ||
) | ||
|
||
// Config stores birdwatcher config items. | ||
type Config struct { | ||
// birdwatcher configuration folder path | ||
// default $PWD/.bw_config | ||
ConfigPath string `yaml:"-"` | ||
// backup workspace path, default $PWD/bw_workspace | ||
WorkspacePath string `yaml:"WorkspacePath"` | ||
} | ||
|
||
func (c *Config) load() error { | ||
err := c.checkConfigPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Open(c.getConfigPath()) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
bs, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return yaml.Unmarshal(bs, c) | ||
} | ||
|
||
func (c *Config) getConfigPath() string { | ||
return path.Join(c.ConfigPath, configFileName) | ||
} | ||
|
||
// checkConfigPath exists and is a directory. | ||
func (c *Config) checkConfigPath() error { | ||
info, err := os.Stat(c.ConfigPath) | ||
if err != nil { | ||
// not exist, return specified type to handle | ||
if os.IsNotExist(err) { | ||
return errConfigPathNotExist | ||
} | ||
return err | ||
} | ||
if !info.IsDir() { | ||
fmt.Printf("%s is not a directory\n", c.ConfigPath) | ||
return fmt.Errorf("%w(%s)", errConfigPathIsFile, configFileName) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Config) createDefault() error { | ||
err := os.MkdirAll(c.ConfigPath, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
file, err := os.Create(c.getConfigPath()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
// setup default value | ||
c.WorkspacePath = defaultWorkspace | ||
|
||
bs, err := yaml.Marshal(c) | ||
if err != nil { | ||
fmt.Println("failed to marshal config", err.Error()) | ||
return err | ||
} | ||
|
||
file.Write(bs) | ||
return nil | ||
} | ||
|
||
func NewConfig(configPath string) (*Config, error) { | ||
config := &Config{ | ||
ConfigPath: configPath, | ||
} | ||
err := config.load() | ||
// config path not exist, may first time to run | ||
if errors.Is(err, errConfigPathNotExist) { | ||
return config, config.createDefault() | ||
} | ||
|
||
return config, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package models | ||
|
||
import "github.com/golang/protobuf/proto" | ||
|
||
// WorkspaceMeta stores birdwatcher workspace information. | ||
type WorkspaceMeta struct { | ||
// Version semver for workspace meta | ||
Version string `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"` | ||
// instance name, as rootPath for key prefix | ||
Instance string `protobuf:"bytes,2,opt,name=instance,proto3" json:"instance,omitempty"` | ||
// MetaPath used in keys | ||
MetaPath string `protobuf:"bytes,3,opt,name=meta_path,proto3" json:"meta_path,omitempty"` | ||
} | ||
|
||
// Reset implements protoiface.MessageV1 | ||
func (v *WorkspaceMeta) Reset() { | ||
*v = WorkspaceMeta{} | ||
} | ||
|
||
// String implements protoiface.MessageV1 | ||
func (v *WorkspaceMeta) String() string { | ||
return proto.CompactTextString(v) | ||
} | ||
|
||
// String implements protoiface.MessageV1 | ||
func (v *WorkspaceMeta) ProtoMessage() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
states/auto_complete_file.go → states/autocomplete/auto_complete_file.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package states | ||
package autocomplete | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package states | ||
package autocomplete | ||
|
||
type cmdCompType int | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package autocomplete | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
) | ||
|
||
func parseInput(input string) inputResult { | ||
// check is end with space | ||
isEndBlank := strings.HasSuffix(input, " ") | ||
|
||
parts := strings.Split(input, " ") | ||
parts = lo.Filter(parts, func(part string, idx int) bool { | ||
return part != "" | ||
}) | ||
|
||
comps := make([]cComp, 0, len(parts)) | ||
currentFlagValue := false | ||
|
||
for _, part := range parts { | ||
// next part is flag value | ||
// just set last comp cValue | ||
if currentFlagValue { | ||
comps[len(comps)-1].cValue = part | ||
currentFlagValue = false | ||
continue | ||
} | ||
// is flag | ||
if strings.HasPrefix(part, "-") { | ||
raw := strings.TrimLeft(part, "-") | ||
if strings.Contains(raw, "=") { | ||
parts := strings.Split(raw, "=") | ||
// a=b | ||
if len(parts) == 2 { | ||
comps = append(comps, cComp{ | ||
raw: part, | ||
cTag: parts[0], | ||
cValue: parts[1], | ||
cType: cmdCompFlag, | ||
}) | ||
} | ||
// TODO handle part len != 2 | ||
} else { | ||
currentFlagValue = true | ||
comps = append(comps, cComp{ | ||
raw: part, | ||
cTag: raw, | ||
cType: cmdCompFlag, | ||
}) | ||
} | ||
} else { | ||
comps = append(comps, cComp{ | ||
raw: part, | ||
cTag: part, | ||
cType: cmdCompCommand, | ||
}) | ||
} | ||
} | ||
|
||
is := inputStateCmd | ||
if currentFlagValue { | ||
if isEndBlank { | ||
is = inputStateFlagValue | ||
} else { | ||
is = inputStateFlagTag | ||
} | ||
} | ||
|
||
// add empty comp if end with space | ||
if isEndBlank { | ||
comps = append(comps, cComp{cType: cmdCompCommand}) | ||
} | ||
|
||
return inputResult{ | ||
parts: comps, | ||
state: is, | ||
} | ||
} |
Oops, something went wrong.