Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125// Package cli implements knit's commands.package cli
import ( "context" "encoding/json" "fmt" "io" "net/http" "os" "runtime/debug" "time"
"github.com/bluesky-social/indigo/atproto/atclient" "github.com/bluesky-social/indigo/atproto/identity" "golang.org/x/term"
"tangled.org/rockorager.dev/knit/internal/config" "tangled.org/rockorager.dev/knit/internal/session" "tangled.org/rockorager.dev/knit/internal/tangled")
// Version is set at build time.var Version = "dev"
// App holds what every command needs: terminal streams, configuration, and a// lazily created API client.type App struct { In io.Reader Out io.Writer Err io.Writer OutTTY bool InTTY bool Config *config.Config RepoFlag string // value of -R/--repo
client *tangled.Client}
func init() { // `go install ...@version` records the module version if info, ok := debug.ReadBuildInfo(); ok && Version == "dev" && info.Main.Version != "" && info.Main.Version != "(devel)" { Version = info.Main.Version }}
func newApp() (*App, error) { cfg, err := config.Load() if err != nil { return nil, err } return &App{ In: os.Stdin, Out: os.Stdout, Err: os.Stderr, OutTTY: term.IsTerminal(int(os.Stdout.Fd())), InTTY: term.IsTerminal(int(os.Stdin.Fd())), Config: cfg, }, nil}
// Client returns the Tangled API client.func (a *App) Client() *tangled.Client { if a.client == nil { session.UserAgent = "knit/" + Version a.client = &tangled.Client{ HTTP: &http.Client{Timeout: 60 * time.Second}, API: a.Config.Get("api"), Appview: a.Config.Get("appview"), UserAgent: session.UserAgent, Dir: identity.DefaultDirectory(), PDSClient: func(ctx context.Context) (*atclient.APIClient, error) { s, err := session.Load() if err != nil { return nil, err } return s.Client(ctx) }, } } return a.client}
// Printf writes to standard output.func (a *App) Printf(format string, args ...any) { fmt.Fprintf(a.Out, format, args...)}
// Infof writes a status message to standard error.func (a *App) Infof(format string, args ...any) { fmt.Fprintf(a.Err, format, args...)}
// Success prints a check-marked status line to standard error.func (a *App) Success(format string, args ...any) { fmt.Fprintf(a.Err, "%s %s\n", a.Green("✓"), fmt.Sprintf(format, args...))}
// PrintJSON writes v as indented JSON.func (a *App) PrintJSON(v any) error { enc := json.NewEncoder(a.Out) enc.SetIndent("", " ") enc.SetEscapeHTML(false) return enc.Encode(v)}
func (a *App) colorEnabled() bool { return a.OutTTY && os.Getenv("NO_COLOR") == "" && os.Getenv("TERM") != "dumb"}
func (a *App) color(code, s string) string { if !a.colorEnabled() { return s } return "\x1b[" + code + "m" + s + "\x1b[0m"}
func (a *App) Bold(s string) string { return a.color("1", s) }func (a *App) Green(s string) string { return a.color("32", s) }func (a *App) Red(s string) string { return a.color("31", s) }func (a *App) Yellow(s string) string { return a.color("33", s) }func (a *App) Magenta(s string) string { return a.color("35", s) }func (a *App) Cyan(s string) string { return a.color("36", s) }func (a *App) Gray(s string) string { return a.color("90", s) }