Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions go/cmd/collect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ import (
)

func main() {
newApp().Run()
result := newApp()
if !result.OK {
core.Error("collect setup failed", "err", result.Value)
core.Exit(1)
return
}
result.Value.(*core.Core).Run()
}

func newApp() *core.Core {
func newApp() core.Result {
app := core.New(core.WithOption("name", "collect"))
app.App().Version = "dev"

_ = app.Command("github", core.Command{Action: github})
_ = app.Command("market", core.Command{Action: market})
_ = app.Command("papers", core.Command{Action: papers})
if r := app.Command("github", core.Command{Action: github}); !r.OK {
return r
}
if r := app.Command("market", core.Command{Action: market}); !r.OK {
return r
}
if r := app.Command("papers", core.Command{Action: papers}); !r.OK {
return r
}

return app
return core.Ok(app)
}

func github(opts core.Options) core.Result {
Expand Down
125 changes: 67 additions & 58 deletions go/cmd/compile/cmd_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
package compile

import (
"os"
"path/filepath"
"strings"

core "dappco.re/go"
"dappco.re/go/scm/manifest"
)
Expand All @@ -22,90 +18,103 @@ func Register(app *core.Core) core.Result {
if app == nil {
return core.Fail(core.E("cmd.compile.Register", "core app is required", nil))
}
return app.Command("compile", core.Command{Action: run})
return app.Command("compile", core.Command{Action: run(app)})
}

func run(opts core.Options) core.Result {
if wantsHelp(opts) {
core.Print(nil, usage)
return core.Ok(nil)
}
func run(app *core.Core) core.CommandAction {
return func(opts core.Options) core.Result {
if wantsHelp(opts) {
core.Print(nil, usage)
return core.Ok(nil)
}

root := option(opts, "root", ".")
manifestPath := option(opts, "manifest", filepath.Join(root, ".core", "manifest.yaml"))
outPath := option(opts, "out", filepath.Join(root, "core.json"))
root := option(opts, "root", ".")
manifestPath := option(opts, "manifest", core.PathJoin(root, ".core", "manifest.yaml"))
outPath := option(opts, "out", core.PathJoin(root, "core.json"))

raw, err := os.ReadFile(manifestPath)
if err != nil {
return failed(err)
}
m, err := manifest.Parse(raw)
if err != nil {
return failed(err)
}
raw, err := readFile(app, manifestPath)
if err != nil {
return failed(err)
}
m, err := manifest.Parse(raw)
if err != nil {
return failed(err)
}

cm, err := manifest.CompileWithOptions(m, manifest.CompileOptions{
Commit: opts.String("commit"),
Tag: opts.String("tag"),
BuiltBy: opts.String("built-by"),
Build: manifest.BuildInfo{
Targets: splitList(option(opts, "targets", opts.String("target"))),
Checksums: opts.String("checksums"),
SHA256: opts.String("sha256"),
},
})
if err != nil {
return failed(err)
}
cm, err := manifest.CompileWithOptions(m, manifest.CompileOptions{
Commit: opts.String("commit"),
Tag: opts.String("tag"),
BuiltBy: opts.String("built-by"),
Build: manifest.BuildInfo{
Targets: splitList(option(opts, "targets", opts.String("target"))),
Checksums: opts.String("checksums"),
SHA256: opts.String("sha256"),
},
})
if err != nil {
return failed(err)
}

compiled, err := manifest.MarshalJSON(cm)
if err != nil {
return failed(err)
}
if err := mkdirParent(outPath); err != nil {
return failed(err)
}
if err := os.WriteFile(outPath, compiled, 0o600); err != nil {
return failed(err)
}
compiled, err := manifest.MarshalJSON(cm)
if err != nil {
return failed(err)
}
if r := app.Fs().WriteMode(outPath, string(compiled), 0o600); !r.OK {
return failed(resultError("cmd.compile.run", "write compiled manifest", r))
}

core.Print(nil, "%s", outPath)
return core.Ok(nil)
core.Print(nil, "%s", outPath)
return core.Ok(nil)
}
}

func option(opts core.Options, key, fallback string) string {
if value := strings.TrimSpace(opts.String(key)); value != "" {
if value := core.Trim(opts.String(key)); value != "" {
return value
}
return fallback
}

func splitList(value string) []string {
if strings.TrimSpace(value) == "" {
if core.Trim(value) == "" {
return nil
}
parts := strings.Split(value, ",")
parts := core.Split(value, ",")
out := make([]string, 0, len(parts))
for _, part := range parts {
if part = strings.TrimSpace(part); part != "" {
if part = core.Trim(part); part != "" {
out = append(out, part)
}
}
return out
}

func mkdirParent(path string) error {
dir := filepath.Dir(path)
if dir == "." || dir == "" {
return nil
}
return os.MkdirAll(dir, 0o755)
}

func wantsHelp(opts core.Options) bool {
return opts.Bool("help") || opts.Bool("h")
}

func failed(err error) core.Result {
return core.Fail(err)
}

func readFile(app *core.Core, path string) ([]byte, error) {
if app == nil {
return nil, core.E("cmd.compile.readFile", "core app is required", nil)
}
r := app.Fs().Read(path)
if !r.OK {
return nil, resultError("cmd.compile.readFile", "read file", r)
}
raw, ok := r.Value.(string)
if !ok {
return nil, core.E("cmd.compile.readFile", "read returned invalid payload", nil)
}
return []byte(raw), nil
}

func resultError(op, msg string, r core.Result) error {
if err, ok := r.Value.(error); ok {
return core.E(op, msg, err)
}
return core.E(op, msg, nil)
}
20 changes: 15 additions & 5 deletions go/cmd/forge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ import (
)

func main() {
newApp().Run()
result := newApp()
if !result.OK {
core.Error("forge setup failed", "err", result.Value)
core.Exit(1)
return
}
result.Value.(*core.Core).Run()
}

func newApp() *core.Core {
func newApp() core.Result {
app := core.New(core.WithOption("name", "forge"))
app.App().Version = "dev"

_ = app.Command("auth", core.Command{Action: auth})
_ = app.Command("repos", core.Command{Action: repos})
if r := app.Command("auth", core.Command{Action: auth}); !r.OK {
return r
}
if r := app.Command("repos", core.Command{Action: repos}); !r.OK {
return r
}

return app
return core.Ok(app)
}

func auth(opts core.Options) core.Result {
Expand Down
25 changes: 17 additions & 8 deletions go/cmd/gitea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ package main

import (
"strconv"
"strings"

core "dappco.re/go"
"dappco.re/go/scm/gitea"
)

func main() {
newApp().Run()
result := newApp()
if !result.OK {
core.Error("gitea setup failed", "err", result.Value)
core.Exit(1)
return
}
result.Value.(*core.Core).Run()
}

func newApp() *core.Core {
func newApp() core.Result {
app := core.New(core.WithOption("name", "gitea"))
app.App().Version = "dev"

_ = app.Command("repos", core.Command{Action: repos})
_ = app.Command("issues", core.Command{Action: issues})
if r := app.Command("repos", core.Command{Action: repos}); !r.OK {
return r
}
if r := app.Command("issues", core.Command{Action: issues}); !r.OK {
return r
}

return app
return core.Ok(app)
}

func repos(opts core.Options) core.Result {
Expand Down Expand Up @@ -97,11 +106,11 @@ func issues(opts core.Options) core.Result {
}

func splitRepo(value string) (string, string) {
parts := strings.SplitN(value, "/", 2)
parts := core.SplitN(value, "/", 2)
if len(parts) != 2 {
return "", ""
}
return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
return core.Trim(parts[0]), core.Trim(parts[1])
}

func intOption(opts core.Options, key string) int {
Expand Down
Loading
Loading