summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.go45
-rw-r--r--example/main.go43
-rw-r--r--helpers.go5
-rw-r--r--repo.proto3
-rw-r--r--sampleConfig.go63
5 files changed, 148 insertions, 11 deletions
diff --git a/config.go b/config.go
index 8cdc9ae..ed13e8c 100644
--- a/config.go
+++ b/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
diff --git a/example/main.go b/example/main.go
index bc6b8a4..058412d 100644
--- a/example/main.go
+++ b/example/main.go
@@ -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
diff --git a/helpers.go b/helpers.go
index 8ddb213..8dc71e9 100644
--- a/helpers.go
+++ b/helpers.go
@@ -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)
diff --git a/repo.proto b/repo.proto
index ae7fc19..f1d307b 100644
--- a/repo.proto
+++ b/repo.proto
@@ -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'
diff --git a/sampleConfig.go b/sampleConfig.go
new file mode 100644
index 0000000..b849468
--- /dev/null
+++ b/sampleConfig.go
@@ -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")
+}