A terminal client for Tangled.
0

Configure Feed

Select the types of activity you want to include in your feed.

config: add viper-backed configuration system

authored by

Aly Raffauf and committed by
Tangled
(Jul 18, 2026, 6:33 PM +0300) ae7d00e5 e8501f50

+156 -19
+39 -1
README.md
··· 119 119 (e.g. a server or container), install and start `gnome-keyring-daemon` or 120 120 `kwalletd`, or set `DBUS_SESSION_BUS_ADDRESS`. 121 121 122 + ## Configuration 123 + 124 + `tg` resolves configuration values from the following sources, in increasing 125 + precedence (later sources override earlier ones): 126 + 127 + 1. **Defaults** — `appview` is `https://bobbin.klbr.net` 128 + 2. **Config file** — `$XDG_CONFIG_HOME/tg/config.toml` (or `~/.config/tg/config.toml`) 129 + 3. **Environment variables** — prefixed `TG_` (e.g. `TG_APPVIEW`) 130 + 4. **Command-line flags** — e.g. `--appview` 131 + 132 + The config file is optional; a missing file is not an error. 133 + 134 + ### Config file 135 + 136 + ```toml 137 + # ~/.config/tg/config.toml 138 + appview = "https://bobbin.klbr.net" 139 + ``` 140 + 141 + Override the config file location with `--config /path/to/config.toml`. 142 + 143 + ### Environment variables 144 + 145 + | Variable | Config key | Purpose | 146 + |---------------|------------|-------------------| 147 + | `TG_APPVIEW` | `appview` | Appview host URL | 148 + 149 + Keys containing `.` or `-` map to `TG_`-prefixed underscore-separated names 150 + (e.g. `foo.bar` → `TG_FOO_BAR`). 151 + 152 + ### Flags 153 + 154 + | Flag | Purpose | 155 + |-------------|------------------------------------------------------------------| 156 + | `--config` | Path to config file | 157 + | `--appview` | Appview host URL (overrides config file and `TG_APPVIEW`) | 158 + 122 159 ## Architecture 123 160 124 161 - `cmd/tg/` — CLI entry point 125 - - `internal/cli/` — Cobra command tree (`repo`, `issue`, `pr`) 162 + - `internal/cli/` — Cobra command tree (`repo`, `issue`, `pr`); Viper-backed configuration 126 163 - `internal/gitutil/` — Git operations (clone, fetch, patch apply) 127 164 - `tangled/` — Typed client for the Bobbin API (`api.tangled.org`) 128 165 - `atproto/` — Identity resolution (handle ↔ DID, PDS discovery); OAuth session storage (system keyring) ··· 133 170 - `git` and `ssh` (for clone and PR checkout) 134 171 - [`github.com/bluesky-social/indigo`](https://github.com/bluesky-social/indigo) — atproto SDK 135 172 - [`github.com/spf13/cobra`](https://github.com/spf13/cobra) — CLI framework 173 + - [`github.com/spf13/viper`](https://github.com/spf13/viper) — configuration 136 174 - [`github.com/zalando/go-keyring`](https://github.com/zalando/go-keyring) — platform keyring access 137 175 138 176 ## License
+13 -3
go.mod
··· 5 5 require ( 6 6 github.com/bluesky-social/indigo v0.0.0-20260629160527-dfe5578fd537 7 7 github.com/spf13/cobra v1.10.2 8 + github.com/spf13/viper v1.21.0 8 9 github.com/zalando/go-keyring v0.2.8 9 10 ) 10 11 ··· 13 14 github.com/cespare/xxhash/v2 v2.2.0 // indirect 14 15 github.com/danieljoos/wincred v1.2.3 // indirect 15 16 github.com/earthboundkid/versioninfo/v2 v2.24.1 // indirect 17 + github.com/fsnotify/fsnotify v1.9.0 // indirect 18 + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect 16 19 github.com/godbus/dbus/v5 v5.2.2 // indirect 17 20 github.com/golang-jwt/jwt/v5 v5.2.2 // indirect 18 - github.com/google/go-cmp v0.6.0 // indirect 19 21 github.com/google/go-querystring v1.1.0 // indirect 20 22 github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect 21 23 github.com/inconshreveable/mousetrap v1.1.0 // indirect ··· 33 35 github.com/multiformats/go-multibase v0.2.0 // indirect 34 36 github.com/multiformats/go-multihash v0.2.3 // indirect 35 37 github.com/multiformats/go-varint v0.0.7 // indirect 38 + github.com/pelletier/go-toml/v2 v2.2.4 // indirect 36 39 github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f // indirect 37 40 github.com/prometheus/client_golang v1.17.0 // indirect 38 41 github.com/prometheus/client_model v0.5.0 // indirect 39 42 github.com/prometheus/common v0.45.0 // indirect 40 43 github.com/prometheus/procfs v0.12.0 // indirect 44 + github.com/sagikazarmark/locafero v0.11.0 // indirect 45 + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect 41 46 github.com/spaolacci/murmur3 v1.1.0 // indirect 42 - github.com/spf13/pflag v1.0.9 // indirect 47 + github.com/spf13/afero v1.15.0 // indirect 48 + github.com/spf13/cast v1.10.0 // indirect 49 + github.com/spf13/pflag v1.0.10 // indirect 50 + github.com/subosito/gotenv v1.6.0 // indirect 43 51 github.com/whyrusleeping/cbor-gen v0.2.1-0.20241030202151-b7a6831be65e // indirect 44 52 gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b // indirect 45 53 gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02 // indirect 54 + go.yaml.in/yaml/v3 v3.0.4 // indirect 46 55 golang.org/x/crypto v0.21.0 // indirect 47 - golang.org/x/sys v0.27.0 // indirect 56 + golang.org/x/sys v0.29.0 // indirect 57 + golang.org/x/text v0.28.0 // indirect 48 58 golang.org/x/time v0.3.0 // indirect 49 59 golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect 50 60 google.golang.org/protobuf v1.33.0 // indirect
+35 -3
go.sum
··· 13 13 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 14 github.com/earthboundkid/versioninfo/v2 v2.24.1 h1:SJTMHaoUx3GzjjnUO1QzP3ZXK6Ee/nbWyCm58eY3oUg= 15 15 github.com/earthboundkid/versioninfo/v2 v2.24.1/go.mod h1:VcWEooDEuyUJnMfbdTh0uFN4cfEIg+kHMuWB2CDCLjw= 16 + github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= 17 + github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= 18 + github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= 19 + github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= 20 + github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= 21 + github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= 16 22 github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= 17 23 github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ= 18 24 github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= ··· 43 49 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= 44 50 github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= 45 51 github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= 52 + github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 53 + github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 54 + github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 55 + github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 46 56 github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg= 47 57 github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= 48 58 github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= ··· 59 69 github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM= 60 70 github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8= 61 71 github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU= 72 + github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= 73 + github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= 62 74 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 63 75 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 64 76 github.com/polydawn/refmt v0.89.1-0.20221221234430-40501e09de1f h1:VXTQfuJj9vKR4TCkEuWIckKvdHFeJH/huIFJ9/cXOB0= ··· 71 83 github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY= 72 84 github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= 73 85 github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= 86 + github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= 87 + github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= 74 88 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 75 89 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 90 + github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= 91 + github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= 76 92 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 77 93 github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= 78 94 github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= 79 95 github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= 80 96 github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= 97 + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= 98 + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U= 81 99 github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= 82 100 github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 101 + github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= 102 + github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= 103 + github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= 104 + github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= 83 105 github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= 84 106 github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= 85 - github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= 86 107 github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 108 + github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= 109 + github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 110 + github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= 111 + github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= 87 112 github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= 88 113 github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 89 114 github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= 90 115 github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= 116 + github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= 117 + github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= 91 118 github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 92 119 github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= 93 120 github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= ··· 99 126 gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b/go.mod h1:/y/V339mxv2sZmYYR64O07VuCpdNZqCTwO8ZcouTMI8= 100 127 gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02 h1:qwDnMxjkyLmAFgcfgTnfJrmYKWhHnci3GjDqcZp1M3Q= 101 128 gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02/go.mod h1:JTnUj0mpYiAsuZLmKjTx/ex3AtMowcCgnE7YNyCEP0I= 129 + go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= 102 130 go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= 103 131 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 104 132 golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= ··· 106 134 golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 107 135 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 108 136 golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 109 - golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= 110 - golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 137 + golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= 138 + golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 111 139 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 140 + golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= 141 + golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= 112 142 golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= 113 143 golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= 114 144 golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= ··· 118 148 google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= 119 149 google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 120 150 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 151 + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 152 + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 121 153 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 122 154 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 123 155 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+61
internal/cli/config.go
··· 1 + package cli 2 + 3 + import ( 4 + "errors" 5 + "fmt" 6 + "os" 7 + "path/filepath" 8 + "strings" 9 + 10 + "github.com/spf13/viper" 11 + ) 12 + 13 + const ( 14 + defaultAppview = "https://bobbin.klbr.net" 15 + configName = "config" 16 + configType = "toml" 17 + ) 18 + 19 + // config resolves values with the following precedence (highest to lowest): 20 + // command-line flags, environment variables prefixed TG_, config file, defaults. 21 + var config = viper.NewWithOptions(viper.KeyDelimiter(".")) 22 + 23 + var configPath string 24 + 25 + func initConfig() { 26 + config.SetConfigName(configName) 27 + config.SetConfigType(configType) 28 + 29 + if configPath != "" { 30 + config.SetConfigFile(configPath) 31 + } else { 32 + for _, dir := range configSearchDirs() { 33 + config.AddConfigPath(dir) 34 + } 35 + } 36 + 37 + config.SetEnvPrefix("TG") 38 + config.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) 39 + config.AutomaticEnv() 40 + config.SetDefault("appview", defaultAppview) 41 + 42 + if err := config.ReadInConfig(); err != nil { 43 + if _, ok := errors.AsType[viper.ConfigFileNotFoundError](err); ok { 44 + // A missing config file is fine; configuration is optional. 45 + return 46 + } 47 + // Surface parse/permission errors but keep running with defaults. 48 + fmt.Fprintln(os.Stderr, "warning: failed to read config:", err) 49 + } 50 + } 51 + 52 + func configSearchDirs() []string { 53 + var dirs []string 54 + if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { 55 + dirs = append(dirs, filepath.Join(xdg, "tg")) 56 + } else if home, err := os.UserHomeDir(); err == nil { 57 + dirs = append(dirs, filepath.Join(home, ".config", "tg")) 58 + } 59 + dirs = append(dirs, ".") 60 + return dirs 61 + }
+8 -12
internal/cli/root.go
··· 2 2 3 3 import ( 4 4 "log/slog" 5 - "os" 6 5 7 6 "github.com/alyraffauf/tg/atproto" 8 7 "github.com/alyraffauf/tg/tangled" ··· 19 18 var ( 20 19 resolver = &atproto.Resolver{Directory: identity.DefaultDirectory()} 21 20 client = &tangled.Tangled{ 22 - Client: &atclient.APIClient{Host: appviewHost()}, 21 + Client: &atclient.APIClient{Host: defaultAppview}, 23 22 Logger: slog.Default(), 24 23 } 25 24 auth = atproto.NewAuthManager(oauthCallbackURL) 26 25 27 26 jsonOutput bool 28 - appview string 29 27 ) 30 28 31 - func appviewHost() string { 32 - if host := os.Getenv("TG_APPVIEW"); host != "" { 33 - return host 34 - } 35 - return "https://bobbin.klbr.net" 36 - } 37 - 38 29 var rootCmd = &cobra.Command{ 39 30 Use: "tg", 40 31 Short: "A CLI for Tangled", 41 32 // Errors such as "not logged in" are expected and shouldn't dump usage. 42 33 SilenceUsage: true, 43 34 PersistentPreRun: func(cmd *cobra.Command, args []string) { 44 - client.Client.Host = appview 35 + client.Client.Host = config.GetString("appview") 45 36 }, 46 37 } 47 38 ··· 50 41 } 51 42 52 43 func init() { 44 + cobra.OnInitialize(initConfig) 45 + 46 + rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Path to config file (default: $XDG_CONFIG_HOME/tg/config.toml)") 53 47 rootCmd.PersistentFlags().BoolVar(&jsonOutput, "json", false, "Output in JSON format") 54 - rootCmd.PersistentFlags().StringVar(&appview, "appview", appviewHost(), "Appview host URL (overrides the TG_APPVIEW environment variable)") 48 + rootCmd.PersistentFlags().String("appview", defaultAppview, "Appview host URL (overrides config file and TG_APPVIEW)") 49 + 50 + config.BindPFlag("appview", rootCmd.PersistentFlags().Lookup("appview")) 55 51 56 52 rootCmd.AddCommand(authCmd) 57 53 authCmd.AddCommand(authLoginCmd)