Documentation ¶
Index ¶
- Constants
- Variables
- func ParseHashConfig(hashConfig string) (hashKey, saltSeparator string, rounds, memCost int, err error)
- func Run(args ...string) error
- type App
- func (app *App) AuthExport(ctx context.Context) (users []*auth.ExportedUserRecord, err error)
- func (app *App) AuthImport(ctx context.Context, users []*auth.ExportedUserRecord, ...) (*auth.UserImportResult, error)
- func (app *App) AuthList(ctx context.Context) (users []*auth.ExportedUserRecord, err error)
- func (app *App) DbExport(ctx context.Context, collectionPaths []string) (collections map[string]map[string]map[string]interface{}, err error)
- func (app *App) DbList(ctx context.Context) (collections []string, err error)
- func (app *App) Login(ctx context.Context, credentialPath string) (err error)
- func (app *App) StorageList(ctx context.Context) (objects []string, err error)
- type Cmd
- func (c *Cmd) Auth(args ...string) error
- func (c *Cmd) Db(args ...string) error
- func (c *Cmd) Eprint(a ...interface{}) (int, error)
- func (c *Cmd) Eprintf(format string, a ...interface{}) (int, error)
- func (c *Cmd) Eprintln(a ...interface{}) (int, error)
- func (c *Cmd) Print(a ...interface{}) (int, error)
- func (c *Cmd) Printf(format string, a ...interface{}) (int, error)
- func (c *Cmd) Println(a ...interface{}) (int, error)
- func (c *Cmd) Run(args ...string) error
- func (c *Cmd) Storage(args ...string) error
- type Runnable
- type RunnableFunc
Examples ¶
Constants ¶
const DefaultCredential = "serviceAccountKey.json"
DefaultCredential is a path to a service account secret key which will be used when neither arguments nor environment variables are specified.
const Version = "0.0.1"
Version of the firebasecli.
Variables ¶
var ( // ErrFailedToParseArgs is returned when args are not parseable. ErrFailedToParseArgs = errors.New("failed to parse args") // ErrUnknownCommand is returned when a given command is unknown/undefined. ErrUnknownCommand = errors.New("unknown command") // ErrFailedToParseHashConfig is returned when hash config file does not follow the format. ErrFailedToParseHashConfig = errors.New("failed to parse hash_config") )
var DefaultCmd = NewCmd()
DefaultCmd holds the default sub commands.
Functions ¶
func ParseHashConfig ¶
func ParseHashConfig(hashConfig string) (hashKey, saltSeparator string, rounds, memCost int, err error)
ParseHashConfig parses hash config following the format below:
hash_config { algorithm: SCRYPT, base64_signer_key: <base64string>, base64_salt_separator: <base64string>, rounds: <integer>, mem_cost: <integer>, }
Types ¶
type App ¶
App represents application. App implements core methods called by CLI, so to reuse or extend commands to fit your workflow, it is recommended to use App instead of Cmd.
func (*App) AuthExport ¶
AuthExport exports the all users.
func (*App) AuthImport ¶
func (app *App) AuthImport(ctx context.Context, users []*auth.ExportedUserRecord, key, saltSeparator string, rounds, memoryCost int) (*auth.UserImportResult, error)
AuthImport imports users.
func (*App) DbExport ¶
func (app *App) DbExport(ctx context.Context, collectionPaths []string) (collections map[string]map[string]map[string]interface{}, err error)
DbExport exports collections.
func (*App) DbList ¶
DbList lists up the all collections.
Example ¶
package main import ( "context" "fmt" "log" "github.com/kazuma1989/firebasecli" ) func main() { app := &firebasecli.App{} app.Login(context.Background(), "/path/to/key.json") collections, err := app.DbList(context.Background()) if err != nil { log.Fatal(err) } fmt.Println(collections) }
Output:
type Cmd ¶
type Cmd struct { // Sub holds sub commands. Sub map[string]Runnable // App holds App. App *App // Stdout holds standard output where a command outputs. // Set nil to suppress output. Stdout io.Writer // Stderr holds standard error where a command outputs. // Set nil to suppress output. Stderr io.Writer }
Cmd executes command.
func NewCmd ¶
func NewCmd() *Cmd
NewCmd returns a new initialized Cmd.
Example ¶
package main import ( "context" "github.com/kazuma1989/firebasecli" ) func main() { // Construct a new Cmd. cmd := firebasecli.NewCmd() // Log in with default credential (e.g. an env var or hard-coded path). cmd.App.Login(context.Background(), "") // Run some command. cmd.Db("list") }
Output:
Example (SuppressStdout) ¶
package main import ( "github.com/kazuma1989/firebasecli" ) func main() { cmd := firebasecli.NewCmd() cmd.Stdout = nil cmd.Stderr = nil cmd.Run("--help") }
Output:
func (*Cmd) Run ¶
Run starts a sub command.
Example ¶
package main import ( "fmt" "github.com/kazuma1989/firebasecli" ) func main() { cmd := firebasecli.NewCmd() cmd.Sub["sub1"] = firebasecli.RunnableFunc(func(args ...string) error { fmt.Println(args) return nil }) // A hack. For testing, avoid logging into Firebase. cmd.App = nil // "opt1" is consumed as a root option so not passed to the sub command. cmd.Run("-c", "opt1", "sub1", "arg1", "arg2") }
Output: [sub1 arg1 arg2]
Example (AddYourCommands) ¶
package main import ( "fmt" "github.com/kazuma1989/firebasecli" ) func main() { firebasecli.DefaultCmd.Sub["foo"] = firebasecli.RunnableFunc(func(args ...string) error { fmt.Println(args) return nil }) firebasecli.DefaultCmd.Run("foo", "arg1", "arg2") // Equivalent to: // firebasecli.Run("foo", "arg1", "arg2") }
Output: [foo arg1 arg2]
Example (ImplMyCmd) ¶
package main import ( "fmt" "github.com/kazuma1989/firebasecli" ) // Define myCmd, extending firebasecli.Cmd. type myCmd firebasecli.Cmd // Override Run method. func (c *myCmd) Run(args ...string) error { if runnable, ok := c.Sub[args[1]]; ok { return runnable.Run(args[1:]...) } return fmt.Errorf("failure from Run") } func main() { cmdFooBar := func(args ...string) error { fmt.Println(args) return fmt.Errorf("failure from foo bar") } cmdFoo := &myCmd{ Sub: make(map[string]firebasecli.Runnable), } cmdFoo.Sub["bar"] = firebasecli.RunnableFunc(cmdFooBar) cmd := firebasecli.NewCmd() cmd.Sub["foo"] = cmdFoo cmd.Run("foo", "bar") }
Output: [bar]
type RunnableFunc ¶
RunnableFunc is a func which implements Runnable.
Example ¶
package main import ( "fmt" "github.com/kazuma1989/firebasecli" ) func main() { cmd := firebasecli.NewCmd() cmd.Sub["sub1"] = firebasecli.RunnableFunc(func(args ...string) error { fmt.Println(args) return nil }) cmd.Run("sub1", "arg1", "arg2") }
Output: [sub1 arg1 arg2]