this repo has no description
at main 111 lines 2.3 kB view raw
1package config 2 3import ( 4 "fmt" 5 6 "filippo.io/age" 7 "github.com/aottr/nox/internal/crypto" 8 "github.com/aottr/nox/internal/state" 9) 10 11type RuntimeOptions struct { 12 ConfigPath string 13 StatePath string 14 IdentityPaths []string 15 DryRun bool 16 Force bool 17 Verbose bool 18 AppName string 19} 20 21type RuntimeContext struct { 22 Config *Config 23 State *state.State 24 Identities []age.Identity 25 App string 26 DryRun bool 27 Force bool 28} 29 30func BuildRuntimeCtxFromConfig(config *Config) (*RuntimeContext, error) { 31 32 if config.StatePath != "" { 33 state.SetPath(config.StatePath) 34 } 35 st, err := state.Load() 36 if err != nil { 37 return nil, err 38 } 39 40 var identityPaths []string 41 // try single identity file first 42 if config.Age.Identity != "" { 43 identityPaths = []string{config.Age.Identity} 44 } else if len(config.Age.Identities) > 0 { 45 identityPaths = config.Age.Identities 46 } else { 47 return nil, fmt.Errorf("no age identites found") 48 } 49 ids, err := crypto.LoadAgeIdentitiesFromPaths(identityPaths) 50 if err != nil { 51 return nil, err 52 } 53 54 return &RuntimeContext{ 55 Config: config, 56 State: st, 57 Identities: ids, 58 DryRun: false, 59 Force: false, 60 }, nil 61} 62 63func BuildRuntimeContext(opts RuntimeOptions) (*RuntimeContext, error) { 64 65 cfg, err := Load(opts.ConfigPath) 66 if err != nil { 67 return nil, err 68 } 69 70 if opts.StatePath != "" { 71 state.SetPath(opts.StatePath) 72 } 73 st, err := state.Load() 74 if err != nil { 75 return nil, err 76 } 77 78 identityPaths := opts.IdentityPaths 79 if len(identityPaths) == 0 { 80 // try single identity file first 81 if cfg.Age.Identity != "" { 82 identityPaths = []string{cfg.Age.Identity} 83 } else if len(cfg.Age.Identities) > 0 { 84 identityPaths = cfg.Age.Identities 85 } else { 86 return nil, fmt.Errorf("no age identites found") 87 } 88 } 89 ids, err := crypto.LoadAgeIdentitiesFromPaths(identityPaths) 90 if err != nil { 91 return nil, err 92 } 93 94 var app string 95 if opts.AppName != "" { 96 if _, exists := cfg.Apps[opts.AppName]; exists { 97 app = opts.AppName 98 } else { 99 return nil, fmt.Errorf("app '%s' not found in configuration", opts.AppName) 100 } 101 } 102 103 return &RuntimeContext{ 104 Config: cfg, 105 State: st, 106 Identities: ids, 107 App: app, 108 DryRun: opts.DryRun, 109 Force: opts.Force, 110 }, nil 111}