feat: persist token_version in config, auto-detect on load
- NetBoxConfig.TokenVersion saved to netssh.yaml by the wizard - config.Load() auto-detects the version from the token prefix if the field is missing (backwards-compatible with existing configs) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -16,8 +17,9 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NetBoxConfig struct {
|
type NetBoxConfig struct {
|
||||||
URL string `mapstructure:"url"`
|
URL string `mapstructure:"url"`
|
||||||
Token string `mapstructure:"token"`
|
Token string `mapstructure:"token"`
|
||||||
|
TokenVersion int `mapstructure:"token_version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResolverConfig struct {
|
type ResolverConfig struct {
|
||||||
@@ -73,6 +75,14 @@ func Load() (*Config, error) {
|
|||||||
return nil, fmt.Errorf("parsing config: %w", err)
|
return nil, fmt.Errorf("parsing config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.NetBox.TokenVersion == 0 && cfg.NetBox.Token != "" {
|
||||||
|
if strings.HasPrefix(cfg.NetBox.Token, "nbt_") {
|
||||||
|
cfg.NetBox.TokenVersion = 2
|
||||||
|
} else {
|
||||||
|
cfg.NetBox.TokenVersion = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.Cache.Path == "" {
|
if cfg.Cache.Path == "" {
|
||||||
cacheDir, err := os.UserCacheDir()
|
cacheDir, err := os.UserCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -103,7 +103,8 @@ func RunWizard(cfg *config.Config) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if netbox.TokenVersion(token) == 1 {
|
tokenVersion := netbox.TokenVersion(token)
|
||||||
|
if tokenVersion == 1 {
|
||||||
fmt.Fprintln(os.Stderr, "\nHinweis: Du verwendest einen Legacy-Token (v1). Erstelle in NetBox einen v2-Token (beginnt mit nbt_) für bessere Kompatibilität.")
|
fmt.Fprintln(os.Stderr, "\nHinweis: Du verwendest einen Legacy-Token (v1). Erstelle in NetBox einen v2-Token (beginnt mit nbt_) für bessere Kompatibilität.")
|
||||||
fmt.Fprintln(os.Stderr, " NetBox → Admin → API Tokens → Add Token")
|
fmt.Fprintln(os.Stderr, " NetBox → Admin → API Tokens → Add Token")
|
||||||
}
|
}
|
||||||
@@ -119,8 +120,9 @@ func RunWizard(cfg *config.Config) error {
|
|||||||
|
|
||||||
out := config.Config{
|
out := config.Config{
|
||||||
NetBox: config.NetBoxConfig{
|
NetBox: config.NetBoxConfig{
|
||||||
URL: strings.TrimRight(strings.TrimSpace(url), "/"),
|
URL: strings.TrimRight(strings.TrimSpace(url), "/"),
|
||||||
Token: strings.TrimSpace(token),
|
Token: strings.TrimSpace(token),
|
||||||
|
TokenVersion: tokenVersion,
|
||||||
},
|
},
|
||||||
SSH: config.SSHConfig{
|
SSH: config.SSHConfig{
|
||||||
DefaultUser: strings.TrimSpace(defaultUser),
|
DefaultUser: strings.TrimSpace(defaultUser),
|
||||||
@@ -148,6 +150,7 @@ func save(cfg config.Config) error {
|
|||||||
b.WriteString("netbox:\n")
|
b.WriteString("netbox:\n")
|
||||||
b.WriteString(fmt.Sprintf(" url: %q\n", cfg.NetBox.URL))
|
b.WriteString(fmt.Sprintf(" url: %q\n", cfg.NetBox.URL))
|
||||||
b.WriteString(fmt.Sprintf(" token: %q\n", cfg.NetBox.Token))
|
b.WriteString(fmt.Sprintf(" token: %q\n", cfg.NetBox.Token))
|
||||||
|
fmt.Fprintf(&b, " token_version: %d\n", cfg.NetBox.TokenVersion)
|
||||||
|
|
||||||
b.WriteString("\nresolver:\n")
|
b.WriteString("\nresolver:\n")
|
||||||
b.WriteString(" strategies:\n")
|
b.WriteString(" strategies:\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user