firebasecli

package module
v0.0.0-...-0900536 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2019 License: MIT Imports: 19 Imported by: 0

README

firebasecli

Firebase Admin CLI.

Getting started

Installation

go get github.com/kazuma1989/firebasecli/firebasecli

Now you can use $GOBIN/firebasecli or just firebasecli (when PATH refers to $GOBIN).

Documentation

Index

Examples

Constants

View Source
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.

View Source
const Version = "0.0.1"

Version of the firebasecli.

Variables

View Source
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")
)
View Source
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>,
}

func Run

func Run(args ...string) error

Run starts the app.

Example
package main

import (
	"fmt"
	"os"

	"github.com/kazuma1989/firebasecli"
)

func main() {
	err := firebasecli.Run(os.Args[1:]...)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	os.Exit(0)
}
Output:

Types

type App

type App struct {
	*firebase.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

func (app *App) AuthExport(ctx context.Context) (users []*auth.ExportedUserRecord, err error)

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) AuthList

func (app *App) AuthList(ctx context.Context) (users []*auth.ExportedUserRecord, err error)

AuthList lists up the all 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

func (app *App) DbList(ctx context.Context) (collections []string, err error)

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:

func (*App) Login

func (app *App) Login(ctx context.Context, credentialPath string) (err error)

Login logs into Firebase with a service account secret key.

Example
package main

import (
	"context"

	"github.com/kazuma1989/firebasecli"
)

func main() {
	app := &firebasecli.App{}
	app.Login(context.Background(), "/path/to/key.json")
}
Output:

func (*App) StorageList

func (app *App) StorageList(ctx context.Context) (objects []string, err error)

StorageList lists up the all collections.

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) Auth

func (c *Cmd) Auth(args ...string) error

Auth manipulates Firebase Authentication.

func (*Cmd) Db

func (c *Cmd) Db(args ...string) error

Db manipulates Cloud Firestore.

func (*Cmd) Eprint

func (c *Cmd) Eprint(a ...interface{}) (int, error)

Eprint prints to stderr.

func (*Cmd) Eprintf

func (c *Cmd) Eprintf(format string, a ...interface{}) (int, error)

Eprintf prints to stderr.

func (*Cmd) Eprintln

func (c *Cmd) Eprintln(a ...interface{}) (int, error)

Eprintln prints to stderr.

func (*Cmd) Print

func (c *Cmd) Print(a ...interface{}) (int, error)

Print prints to stdout.

func (*Cmd) Printf

func (c *Cmd) Printf(format string, a ...interface{}) (int, error)

Printf prints to stdout.

func (*Cmd) Println

func (c *Cmd) Println(a ...interface{}) (int, error)

Println prints to stdout.

func (*Cmd) Run

func (c *Cmd) Run(args ...string) error

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]

func (*Cmd) Storage

func (c *Cmd) Storage(args ...string) error

Storage manipulates Cloud Storge

type Runnable

type Runnable interface {
	Run(...string) error
}

Runnable runs a command.

type RunnableFunc

type RunnableFunc func(...string) error

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]

func (RunnableFunc) Run

func (f RunnableFunc) Run(args ...string) error

Run implements Runnable.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL