Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cmd/cue/cmd: allow passing args to API
Browse files Browse the repository at this point in the history
doesn't fully work yet because commands and
flags are still global.

Issue #50

Change-Id: I281684d0a9bfc72c9c852cf17f1552587e127efb
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2160
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed Jun 7, 2019
1 parent bb2b651 commit 0c3f5c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
14 changes: 8 additions & 6 deletions cmd/cue/cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ func runCommand(t *testing.T, f func(cmd *cobra.Command, args []string) error, n
g.Go(func() error {
defer wOut.Close()
defer func() {
if e := recover(); e != nil {
if err, ok := e.(error); ok {
errors.Print(wOut, err)
} else {
fmt.Fprintln(wOut, e)
}
switch err := recover().(type) {
case nil:
case panicError:
errors.Print(wOut, err.Err)
case error:
errors.Print(wOut, err)
default:
fmt.Fprintln(wOut, err)
}
}()
cmd.Execute()
Expand Down
41 changes: 19 additions & 22 deletions cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"context"
"errors"
"fmt"
logger "log"
"os"
Expand Down Expand Up @@ -67,27 +69,26 @@ For more information on writing CUE configuration files see cuelang.org.`,
SilenceUsage: true,
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
// Main runs the cue tool. It loads the tool flags.
func Main(ctx context.Context, args []string) (err error) {
log.SetFlags(0)
// Three categories of commands:
// - normal
// - user defined
// - help
// For the latter two, we need to use the default loading.
defer func() {
switch err := recover(); err {
switch e := recover().(type) {
case nil:
case panicSentinel:
log.Fatal(err)
os.Exit(1)
case panicError:
err = e.Err
default:
panic(err)
}
// We use panic to escape, instead of os.Exit
}()
if args := os.Args[1:]; len(args) >= 1 && args[0] != "help" {
rootCmd.SetArgs(args)
if len(args) >= 1 && args[0] != "help" {
// TODO: for now we only allow one instance. Eventually, we can allow
// more if they all belong to the same package and we merge them
// before computing commands.
Expand All @@ -112,11 +113,13 @@ func Execute() {
// list available commands
commands := tools.Lookup(sub.name)
i, err := commands.Fields()
must(err)
if err != nil {
return err
}
for i.Next() {
_, _ = addCustom(sub.cmd, sub.name, i.Label(), tools)
}
return // TODO: will this trigger the help?
return nil
}
tools := buildTools(rootCmd, args[1:])
_, err := addCustom(sub.cmd, sub.name, args[0], tools)
Expand All @@ -126,22 +129,16 @@ func Execute() {
}
}
}
if err := rootCmd.Execute(); err != nil {
// log.Fatal(err)
os.Exit(1)
}
return rootCmd.Execute()
}

var panicSentinel = "terminating because of errors"

func must(err error) {
if err != nil {
log.Print(err)
exit()
}
type panicError struct {
Err error
}

func exit() { panic(panicSentinel) }
func exit() {
panic(panicError{errors.New("terminating because of errors")})
}

func init() {
cobra.OnInitialize(initConfig)
Expand Down
14 changes: 12 additions & 2 deletions cmd/cue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@

package main

import "cuelang.org/go/cmd/cue/cmd"
import (
"context"
"log"
"os"

"cuelang.org/go/cmd/cue/cmd"
)

func main() {
cmd.Execute()
err := cmd.Main(context.Background(), os.Args[1:])
if err != nil {
log.Println(err)
os.Exit(1)
}
}

0 comments on commit 0c3f5c6

Please sign in to comment.