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
411 changes: 411 additions & 0 deletions cmd/api_keys.go

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions cmd/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"encoding/json"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"

"github.com/requestyai/cli/internal/client"
"github.com/shopspring/decimal"
"github.com/spf13/cobra"
)

// jsonOutput reports whether the caller asked for machine-readable output. The
// flag is declared once on the parent command and inherited from there.
func jsonOutput(cmd *cobra.Command) bool {
value, err := cmd.Flags().GetBool(jsonFlag)
return err == nil && value
}

// printJSON writes v as indented JSON, which is the contract scripts rely on.
func printJSON(w io.Writer, v any) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")

return encoder.Encode(v)
}

// writeTable prints rows under headers, each column as wide as its widest cell.
func writeTable(w io.Writer, headers []string, rows [][]string) error {
writer := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)

if _, err := fmt.Fprintln(writer, strings.Join(headers, "\t")); err != nil {
return err
}

for _, row := range rows {
if _, err := fmt.Fprintln(writer, strings.Join(row, "\t")); err != nil {
return err
}
}

return writer.Flush()
}

// writeFields prints label and value pairs, one per line, for a single record.
func writeFields(w io.Writer, fields [][2]string) error {
writer := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)

for _, field := range fields {
if _, err := fmt.Fprintf(writer, "%s\t%s\n", field[0], field[1]); err != nil {
return err
}
}

return writer.Flush()
}

// formatMoney renders an amount in dollars and cents.
func formatMoney(amount decimal.Decimal) string {
return "$" + amount.StringFixed(2)
}

// formatLimit renders a spending cap, where zero means there is none.
func formatLimit(limit decimal.Decimal) string {
if limit.IsZero() {
return "unlimited"
}

return formatMoney(limit)
}

// formatLabels renders labels as sorted key=value pairs.
func formatLabels(labels map[string]string) string {
if len(labels) == 0 {
return "-"
}

pairs := make([]string, 0, len(labels))
for key, value := range labels {
pairs = append(pairs, key+"="+value)
}
sort.Strings(pairs)

return strings.Join(pairs, " ")
}

// formatPermissions renders what a key is allowed to do.
func formatPermissions(permissions client.APIKeyPermissions) string {
return fmt.Sprintf("manage=%s completions=%s", permissions.Manage, permissions.Completions)
}

// formatGroup renders the group a key belongs to, if it belongs to one.
func formatGroup(group *client.APIKeyGroup) string {
if group == nil || group.ID == "" {
return "-"
}

return group.ID
}
51 changes: 46 additions & 5 deletions cmd/requesty.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,61 @@ import (
"fmt"

tea "charm.land/bubbletea/v2"
"github.com/requestyai/cli/internal/client"
"github.com/requestyai/cli/internal/config"
"github.com/requestyai/cli/internal/tui"
"github.com/spf13/cobra"
)

// Run starts the Requesty terminal UI.
// Run executes the requesty command line.
func Run() error {
env, err := newEnvironment()
if err != nil {
return fmt.Errorf("failed to initialize environment: %w", err)
}

return newRootCommand(env).Execute()
}

// environment is everything the commands need from the outside world, kept
// behind function fields so tests can stand in for the gateway and the UI.
type environment struct {
config config.Config
apiv2Client *client.Client
}

func newEnvironment() (environment, error) {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
return environment{}, fmt.Errorf("failed to load config: %w", err)
}

if _, err := tea.NewProgram(tui.NewRoot(cfg)).Run(); err != nil {
return fmt.Errorf("failed to run program: %w", err)
return environment{
config: cfg,
apiv2Client: client.New(cfg),
}, nil
}

func newRootCommand(env environment) *cobra.Command {
root := &cobra.Command{
Use: "requesty",
Short: "Point your AI coding harnesses at Requesty",
Long: "Requesty routes every AI coding harness on your machine through one gateway.\n\n" +
"Run with no arguments for the terminal app that configures harnesses and shows\n" +
"what you are spending. The subcommands manage your organization instead.",
Args: cobra.NoArgs,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(_ *cobra.Command, _ []string) error {
if _, err := tea.NewProgram(tui.NewRoot(env.config)).Run(); err != nil {
return fmt.Errorf("failed to run program: %w", err)
}

return nil
},
}

return nil
root.AddCommand(newAPIKeysCommand(env))

return root
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/charmbracelet/x/ansi v0.11.7
github.com/pelletier/go-toml/v2 v2.4.3
github.com/shopspring/decimal v1.4.0
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -24,12 +25,14 @@ require (
github.com/clipperhouse/displaywidth v0.11.0 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lrstanley/bubblezone/v2 v2.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
github.com/mattn/go-runewidth v0.0.24 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.21.0 // indirect
golang.org/x/sys v0.46.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSE
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lrstanley/bubblezone/v2 v2.0.0 h1:pMb9fHKs0slJF6OrzQ2hEgWusqyl9VU/S0UZ5hyh7ZA=
github.com/lrstanley/bubblezone/v2 v2.0.0/go.mod h1:yV/QTjcm4Zu5cqvGvdHi7xVUfnB36w/SafOuDp57dgY=
github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4=
Expand All @@ -44,12 +47,19 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
Expand Down
Loading
Loading