config files are cool!
This commit is contained in:
parent
35a2db858f
commit
39a8d9e13e
45
config.go
45
config.go
|
@ -9,7 +9,6 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// writes out the cluster information it seperate files
|
||||
|
@ -29,18 +28,50 @@ func (m *Repos) ConfigSave() error {
|
|||
}
|
||||
|
||||
func (m *Repos) ConfigLoad() error {
|
||||
var data []byte
|
||||
var err error
|
||||
if m == nil {
|
||||
return errors.New("It's not safe to run ConfigLoad() on a nil ?")
|
||||
}
|
||||
|
||||
if data, err := loadFile("forge.pb"); err == nil {
|
||||
if err = proto.Unmarshal(data, m); err != nil {
|
||||
if data, err = loadFile("forge.pb"); err != nil {
|
||||
// something went wrong loading the file
|
||||
return err
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
// this means the forge.pb file exists and was read
|
||||
if len(data) == 0 {
|
||||
// todo: error out if the file is empty?
|
||||
// try forge.text & forge.json?
|
||||
}
|
||||
if err = m.Unmarshal(data); err != nil {
|
||||
log.Warn("broken forge.pb config file")
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// forge.db doesn't exist. try forge.text
|
||||
// this lets the user hand edit the config
|
||||
if data, err = loadFile("forge.text"); err != nil {
|
||||
// something went wrong loading the file
|
||||
return err
|
||||
}
|
||||
|
||||
if data != nil {
|
||||
// this means the forge.text file exists and was read
|
||||
if len(data) == 0 {
|
||||
// todo: error out if the file is empty?
|
||||
}
|
||||
if err = m.UnmarshalTEXT(data); err != nil {
|
||||
log.Warn("broken forge.text config file")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// first time user. make a template config file
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -49,6 +80,12 @@ func loadFile(filename string) ([]byte, error) {
|
|||
p := filepath.Join(homeDir, ".config/forge")
|
||||
fullname := filepath.Join(p, filename)
|
||||
data, err := os.ReadFile(fullname)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
// if file does not exist, just return nil. this
|
||||
// will cause ConfigLoad() to try the next config file like "forge.text"
|
||||
// because the user might want to edit the .config by hand
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
// log.Info("open config file :", err)
|
||||
return nil, err
|
||||
|
|
|
@ -2,21 +2,24 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"go.wit.com/lib/protobuf/forgepb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
repos := testRepos()
|
||||
var repos *forgepb.Repos
|
||||
repos = new(forgepb.Repos)
|
||||
if err := repos.ConfigLoad(); err != nil {
|
||||
log.Warn("forgepb.ConfigLoad() failed", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
testAddRepos(repos)
|
||||
repos.ConfigSave()
|
||||
}
|
||||
|
||||
func testRepos() *forgepb.Repos {
|
||||
var all *forgepb.Repos
|
||||
all = new(forgepb.Repos)
|
||||
// r = forgepb.LoadJSON("go.wit.com/lib/protobuf/forgepb")
|
||||
|
||||
func testAddRepos(all *forgepb.Repos) *forgepb.Repos {
|
||||
new1 := new(forgepb.Repo)
|
||||
new1.Name = "bash"
|
||||
new1.Version = "5.2.21"
|
||||
|
@ -26,6 +29,34 @@ func testRepos() *forgepb.Repos {
|
|||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new1 = new(forgepb.Repo)
|
||||
new1.Name = "zookeeper"
|
||||
new1.Debname = "zookeeper-go"
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new1 = new(forgepb.Repo)
|
||||
new1.Name = "wit-package"
|
||||
new1.Private = true
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new1 = new(forgepb.Repo)
|
||||
new1.Name = "networkQuality"
|
||||
new1.Debname = "networkquality"
|
||||
new1.Readonly = true
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new2 := new(forgepb.Repo)
|
||||
new2.Name = "go-clone"
|
||||
new2.Version = "0.6.8" // good version of the macos
|
||||
|
|
|
@ -24,6 +24,11 @@ func (p *Repos) FormatTEXT() string {
|
|||
return prototext.Format(p)
|
||||
}
|
||||
|
||||
// unmarshalTEXT
|
||||
func (p *Repos) UnmarshalTEXT(data []byte) error {
|
||||
return prototext.Unmarshal(data, p)
|
||||
}
|
||||
|
||||
// marshal json
|
||||
func (p *Repos) MarshalJSON() ([]byte, error) {
|
||||
return protojson.Marshal(p)
|
||||
|
|
|
@ -19,7 +19,8 @@ message Repo {
|
|||
bool readonly = 6; // if you have write access to the repo
|
||||
bool private = 7; // if the repo can be published
|
||||
string debname = 8; // this is the actual .deb name of the package
|
||||
google.protobuf.Timestamp verstamp = 9; // the git commit timestamp of the version
|
||||
string gopath = 9; // Examples: 'go.wit.com/apps/go-clone' or "~/mythings" or "/home/src/foo"
|
||||
google.protobuf.Timestamp verstamp = 10; // the git commit timestamp of the version
|
||||
}
|
||||
|
||||
// TODO: autogen 'Repos'
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
package forgepb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
func (all *Repos) SampleConfig() {
|
||||
new1 := new(Repo)
|
||||
new1.Name = "bash"
|
||||
new1.Version = "5.2.21"
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new1 = new(Repo)
|
||||
new1.Name = "zookeeper"
|
||||
new1.Debname = "zookeeper-go"
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new1 = new(Repo)
|
||||
new1.Name = "wit-package"
|
||||
new1.Private = true
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new1 = new(Repo)
|
||||
new1.Name = "networkQuality"
|
||||
new1.Debname = "networkquality"
|
||||
new1.Readonly = true
|
||||
if all.Append(new1) {
|
||||
log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
|
||||
new2 := new(Repo)
|
||||
new2.Name = "go-clone"
|
||||
new2.Version = "0.6.8" // good version of the macos
|
||||
if all.Append(new2) {
|
||||
log.Info("added", new2.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new2.Name, "failed")
|
||||
}
|
||||
|
||||
if all.Append(new2) {
|
||||
log.Info("added", new2.Name, "ok (this is bad)")
|
||||
} else {
|
||||
log.Info("added", new2.Name, "failed (but ok)")
|
||||
}
|
||||
|
||||
fmt.Println("first time user. adding an example config file with", len(all.Repos), "repos")
|
||||
}
|
Loading…
Reference in New Issue