diff --git a/cmd/bore/app/handler.go b/cmd/bore/app/handler.go index 1a5ecb9..f90a9b1 100644 --- a/cmd/bore/app/handler.go +++ b/cmd/bore/app/handler.go @@ -3,7 +3,6 @@ package app import ( "bufio" "bytes" - "errors" "fmt" "io" "os" @@ -64,7 +63,7 @@ func (a *App) Copy(ctx *cli.Context) error { return fmt.Errorf("unsupported format: %s", format) } - var fn func(*cli.Context) (io.Reader, error) = a.CopyFromPrompt + fn := a.CopyFromPrompt // If the program was piped to, read directly from STDIN fi, err := os.Stdin.Stat() @@ -75,11 +74,6 @@ func (a *App) Copy(ctx *cli.Context) error { fn = a.CopyFromStdin } - // Sanity check - if fn == nil { - return errors.New("unsupported input method") - } - reader, err := fn(ctx) if err != nil { return err @@ -91,7 +85,7 @@ func (a *App) Copy(ctx *cli.Context) error { } if a.config.ShowIdOnCopy { - fmt.Fprintln(ctx.App.Writer, fmt.Sprintf("Copied content with ID: %s", id)) + fmt.Fprintf(ctx.App.Writer, "Copied content with ID: %s", id) } return nil diff --git a/cmd/bore/root.go b/cmd/bore/root.go index 7596dcd..a0089fc 100644 --- a/cmd/bore/root.go +++ b/cmd/bore/root.go @@ -6,16 +6,17 @@ import ( "github.com/urfave/cli/v2" boreapp "go.trulyao.dev/bore/cmd/bore/app" "go.trulyao.dev/bore/pkg/config" + "go.trulyao.dev/bore/pkg/handler" ) var ( app *boreapp.App - err error version = "latest" ) func Execute() error { + var err error app, err = boreapp.New(config.DefaultConfigFilePath()) if err != nil { return err @@ -31,8 +32,13 @@ func CreateRootCommand() *cli.App { Usage: "A minimal clipboard manager for terminal/headless environments", Version: version, Action: func(c *cli.Context) error { - cli.ShowAppHelp(c) - return nil + // If the program was piped to, read directly from STDIN + fi, _ := os.Stdin.Stat() + if (fi.Mode() & os.ModeCharDevice) == 0 { + return app.Copy(c) + } + + return app.Paste(c) }, Flags: []cli.Flag{ &cli.StringFlag{ @@ -45,6 +51,12 @@ func CreateRootCommand() *cli.App { Name: "json", Usage: "Output the result in JSON format", }, + &cli.StringFlag{ + Name: "format", + Aliases: []string{"f"}, + Usage: "The format of the content to copy. Available formats: base64, plain-text", + Value: handler.FormatPlainText.String(), + }, }, // Global flags have to be passed in BEFORE subcommands e.g `bore -c config.toml config dump`