Skip to content

Commit

Permalink
feat: support configuration via env var or config file
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Feb 24, 2024
1 parent f6c7982 commit e752936
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
16 changes: 11 additions & 5 deletions cliphist.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "image/jpeg"
_ "image/png"

"go.senan.xyz/flagconf"
_ "golang.org/x/image/bmp"

bolt "go.etcd.io/bbolt"
Expand All @@ -28,21 +29,26 @@ import (
//go:embed version.txt
var version string

//nolint:errcheck
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage:\n")
fmt.Fprintf(os.Stderr, " $ %s <store|list|decode|delete|delete-query|wipe|version>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "options:\n")
fmt.Fprintf(flag.CommandLine.Output(), "usage:\n")
fmt.Fprintf(flag.CommandLine.Output(), " $ %s <store|list|decode|delete|delete-query|wipe|version>\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "options:\n")
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(os.Stderr, " -%s (default %s)\n", f.Name, f.DefValue)
fmt.Fprintf(os.Stderr, " %s\n", f.Usage)
fmt.Fprintf(flag.CommandLine.Output(), " -%s (default %s)\n", f.Name, f.DefValue)
fmt.Fprintf(flag.CommandLine.Output(), " %s\n", f.Usage)
})
}

maxItems := flag.Uint64("max-items", 750, "maximum number of items to store")
maxDedupeSearch := flag.Uint64("max-dedupe-search", 100, "maximum number of last items to look through when finding duplicates")
previewWidth := flag.Uint("preview-width", 100, "maximum number of characters to preview")
configPath := flag.String("config-path", "$XDG_CONFIG_HOME/cliphist/config", "overwrite config path to use instead of cli flags")

flag.Parse()
flagconf.ParseEnv()
flagconf.ParseConfig(*configPath)

var err error
switch flag.Arg(0) {
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module go.senan.xyz/cliphist
go 1.20

require (
github.com/rogpeppe/go-internal v1.10.0
go.etcd.io/bbolt v1.3.8
github.com/rogpeppe/go-internal v1.12.0
go.etcd.io/bbolt v1.3.9
go.senan.xyz/flagconf v0.1.4
golang.org/x/image v0.15.0
)

require golang.org/x/sys v0.17.0 // indirect
require (
golang.org/x/sys v0.17.0 // indirect
golang.org/x/tools v0.14.0 // indirect
)
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.etcd.io/bbolt v1.3.9 h1:8x7aARPEXiXbHmtUwAIv7eV2fQFHrLLavdiJ3uzJXoI=
go.etcd.io/bbolt v1.3.9/go.mod h1:zaO32+Ti0PK1ivdPtgMESzuzL2VPoIG1PCQNvOdo/dE=
go.senan.xyz/flagconf v0.1.4 h1:j92ZqK4o299UBH2wVjhV6VevyhIHwrPSkzgj/+ShDCA=
go.senan.xyz/flagconf v0.1.4/go.mod h1:CGD/sgYWiTacz1ojgsQRwErqLxtShWMpBxxnsJI6yaE=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
14 changes: 14 additions & 0 deletions testdata/flagconf.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exec randstr 100
stdin stdout
exec cliphist store

env CLIPHIST_PREVIEW_WIDTH=10
exec cliphist list
stdout '^.\t.{10}…$'

env CLIPHIST_PREVIEW_WIDTH=
exec cliphist -config-path conf list
stdout '^.\t.{20}…$'

-- conf --
preview-width 20

0 comments on commit e752936

Please sign in to comment.