Exemplo n.º 1
0
func tokensList(c *cli.Context) int {
	// Connect to RethinkDB
	_, session, connected := connectToRethinkDB(c)
	if !connected {
		return 1
	}

	// Get tokens from database
	cursor, err := r.Table("tokens").Map(func(row r.Term) r.Term {
		return r.Branch(
			row.HasFields("client_id"),
			row.Merge(map[string]interface{}{
				"owners_address": r.Table("accounts").Get(row.Field("owner")).Field("main_address"),
				"client_name":    r.Table("applications").Get(row.Field("client_id")).Field("name"),
			}),
			row.Merge(map[string]interface{}{
				"owners_address": r.Table("accounts").Get(row.Field("owner")).Field("main_address"),
			}),
		)
	}).Run(session)
	if err != nil {
		writeError(c, err)
		return 1
	}
	var tokens []struct {
		models.Token
		OwnersAddress string `gorethink:"owners_address" json:"owners_address"`
		ClientName    string `gorethink:"client_name" json:"client_name,omitempty"`
	}
	if err := cursor.All(&tokens); err != nil {
		writeError(c, err)
		return 1
	}

	// Write the output
	if c.Bool("json") {
		if err := json.NewEncoder(c.App.Writer).Encode(tokens); err != nil {
			writeError(c, err)
			return 1
		}

		fmt.Fprint(c.App.Writer, "\n")
	} else {
		table := termtables.CreateTable()
		table.AddHeaders("id", "type", "owner", "client_name", "expired", "date_created")
		for _, token := range tokens {
			table.AddRow(
				token.ID,
				token.Type,
				token.OwnersAddress,
				token.ClientName,
				!token.ExpiryDate.IsZero() && token.ExpiryDate.Before(time.Now()),
				token.DateCreated.Format(time.RubyDate),
			)
		}
		fmt.Fprintln(c.App.Writer, table.Render())
	}

	return 0
}
Exemplo n.º 2
0
func applicationsList(c *cli.Context) int {
	// Connect to RethinkDB
	_, session, connected := connectToRethinkDB(c)
	if !connected {
		return 1
	}

	// Get applications from database
	cursor, err := r.Table("applications").Map(func(row r.Term) r.Term {
		return row.Merge(map[string]interface{}{
			"owners_address": r.Table("accounts").Get(row.Field("owner")).Field("main_address"),
		})
	}).Run(session)
	if err != nil {
		writeError(c, err)
		return 1
	}
	var applications []struct {
		models.Application
		OwnersAddress string `gorethink:"owners_address" json:"owners_address"`
	}
	if err := cursor.All(&applications); err != nil {
		writeError(c, err)
		return 1
	}

	// Write the output
	if c.Bool("json") {
		if err := json.NewEncoder(c.App.Writer).Encode(applications); err != nil {
			writeError(c, err)
			return 1
		}

		fmt.Fprint(c.App.Writer, "\n")
	} else {
		table := termtables.CreateTable()
		table.AddHeaders("id", "name", "owner", "homepage", "date_created")
		for _, application := range applications {
			table.AddRow(
				application.ID,
				application.Name,
				application.OwnersAddress,
				application.Homepage,
				application.DateCreated.Format(time.RubyDate),
			)
		}
		fmt.Fprintln(c.App.Writer, table.Render())
	}

	return 0
}
Exemplo n.º 3
0
func accountsList(c *cli.Context) int {
	// Connect to RethinkDB
	_, session, connected := connectToRethinkDB(c)
	if !connected {
		return 1
	}

	// Get accounts without passwords from database
	cursor, err := r.Table("accounts").Map(func(row r.Term) r.Term {
		return row.Without("password").Merge(map[string]interface{}{
			"addresses": r.Table("addresses").GetAllByIndex("owner", row.Field("id")).CoerceTo("array"),
		})
	}).Run(session)
	if err != nil {
		writeError(c, err)
		return 1
	}
	var accounts []struct {
		models.Account
		Addresses []*models.Address `gorethink:"addresses" json:"addresses`
	}
	if err := cursor.All(&accounts); err != nil {
		writeError(c, err)
		return 1
	}

	// Write the output
	if c.Bool("json") {
		if err := json.NewEncoder(c.App.Writer).Encode(accounts); err != nil {
			writeError(c, err)
			return 1
		}

		fmt.Fprint(c.App.Writer, "\n")
	} else {
		table := termtables.CreateTable()
		table.AddHeaders("id", "addresses", "subscription", "status", "date_created")
		for _, account := range accounts {
			emails := []string{}

			for _, address := range account.Addresses {
				if address.ID == account.MainAddress {
					address.StyledID = "*" + address.StyledID
					emails = append([]string{address.StyledID}, emails...)
				} else {
					emails = append(emails, address.StyledID)
				}
			}

			table.AddRow(
				account.ID,
				strings.Join(emails, ", "),
				account.Subscription,
				account.Status,
				account.DateCreated.Format(time.RubyDate),
			)
		}
		fmt.Fprintln(c.App.Writer, table.Render())
	}

	return 0
}