@@ -35,62 +35,62 @@ const (
3535 clientName = "ttn-lw-migrate"
3636)
3737
38- func New () ( * Config , * pflag. FlagSet ) {
39- var (
40- config = & Config { sdkConfig : ttnsdk .NewCommunityConfig (clientName )}
41- flags = & pflag.FlagSet {}
42- )
38+ func NewConfig () * Config {
39+ config := & Config {
40+ sdkConfig : ttnsdk .NewCommunityConfig (clientName ),
41+ flags : & pflag.FlagSet {},
42+ }
4343
44- flags .StringVar (& config .frequencyPlanID ,
44+ config . flags .StringVar (& config .frequencyPlanID ,
4545 "frequency-plan-id" ,
4646 os .Getenv ("FREQUENCY_PLAN_ID" ),
4747 "Frequency Plan ID of exported devices" )
48- flags .StringVar (& config .appID ,
48+ config . flags .StringVar (& config .appID ,
4949 "app-id" ,
5050 os .Getenv ("TTNV2_APP_ID" ),
5151 "TTN Application ID" )
52- flags .StringVar (& config .appAccessKey ,
52+ config . flags .StringVar (& config .appAccessKey ,
5353 "app-access-key" ,
54- os . Getenv ( "TTNV2_APP_ACCESS_KEY" ) ,
54+ "" ,
5555 "TTN Application Access Key (with 'devices' permissions" )
56- flags .StringVar (& config .caCert ,
56+ config . flags .StringVar (& config .caCert ,
5757 "ca-cert" ,
5858 os .Getenv ("TTNV2_CA_CERT" ),
5959 "(only for private networks)" )
60- flags .StringVar (& config .sdkConfig .HandlerAddress ,
60+ config . flags .StringVar (& config .sdkConfig .HandlerAddress ,
6161 "handler-address" ,
6262 os .Getenv ("TTNV2_HANDLER_ADDRESS" ),
6363 "(only for private networks) Address for the Handler" )
64- flags .StringVar (& config .sdkConfig .AccountServerAddress ,
64+ config . flags .StringVar (& config .sdkConfig .AccountServerAddress ,
6565 "account-server-address" ,
6666 os .Getenv ("TTNV2_ACCOUNT_SERVER_ADDRESS" ),
6767 "(only for private networks) Address for the Account Server" )
68- flags .StringVar (& config .sdkConfig .AccountServerClientID ,
68+ config . flags .StringVar (& config .sdkConfig .AccountServerClientID ,
6969 "account-server-client-id" ,
7070 os .Getenv ("TTNV2_ACCOUNT_SERVER_CLIENT_ID" ),
7171 "(only for private networks) Client ID for the Account Server" )
72- flags .StringVar (& config .sdkConfig .AccountServerClientSecret ,
72+ config . flags .StringVar (& config .sdkConfig .AccountServerClientSecret ,
7373 "account-server-client-secret" ,
74- os . Getenv ( "TTNV2_ACCOUNT_SERVER_CLIENT_SECRET" ) ,
74+ "" ,
7575 "(only for private networks) Client secret for the Account Server" )
76- flags .StringVar (& config .sdkConfig .DiscoveryServerAddress ,
76+ config . flags .StringVar (& config .sdkConfig .DiscoveryServerAddress ,
7777 "discovery-server-address" ,
7878 os .Getenv ("TTNV2_DISCOVERY_SERVER_ADDRESS" ),
7979 "(only for private networks) Address for the Discovery Server" )
80- flags .BoolVar (& config .sdkConfig .DiscoveryServerInsecure ,
80+ config . flags .BoolVar (& config .sdkConfig .DiscoveryServerInsecure ,
8181 "discovery-server-insecure" ,
82- false ,
82+ os . Getenv ( "TTNV2_DISCOVERY_SERVER_INSECURE" ) == "true" ,
8383 "(only for private networks) Not recommended" )
84- flags .BoolVar (& config .withSession ,
84+ config . flags .BoolVar (& config .withSession ,
8585 "with-session" ,
86- true ,
86+ os . Getenv ( "TTNV2_WITH_SESSION" ) == " true" ,
8787 "Export device session keys and frame counters" )
88- flags .BoolVar (& config .resetsToFrequencyPlan ,
88+ config . flags .BoolVar (& config .resetsToFrequencyPlan ,
8989 "resets-to-frequency-plan" ,
90- false ,
90+ os . Getenv ( "TTNV2_RESETS_TO_FREQUENCY_PLAN" ) == "true" ,
9191 "Configure preset frequencies for ABP devices so that they match the used Frequency Plan" )
9292
93- return config , flags
93+ return config
9494}
9595
9696type Config struct {
@@ -106,10 +106,18 @@ type Config struct {
106106 dryRun bool
107107 resetsToFrequencyPlan bool
108108
109+ flags * pflag.FlagSet
109110 fpStore * frequencyplans.Store
110111}
111112
112113func (c * Config ) Initialize (rootConfig source.Config ) error {
114+ if appAccessKey := os .Getenv ("TTNV2_APP_ACCESS_KEY" ); appAccessKey != "" && c .appAccessKey == "" {
115+ c .appAccessKey = appAccessKey
116+ }
117+ if accountServerClientSecret := os .Getenv ("TTNV2_ACCOUNT_SERVER_CLIENT_SECRET" ); accountServerClientSecret != "" && c .sdkConfig .AccountServerClientSecret == "" {
118+ c .sdkConfig .AccountServerClientSecret = accountServerClientSecret
119+ }
120+
113121 if c .caCert != "" {
114122 if c .sdkConfig .TLSConfig == nil {
115123 c .sdkConfig .TLSConfig = new (tls.Config )
@@ -128,6 +136,9 @@ func (c *Config) Initialize(rootConfig source.Config) error {
128136 rootCAs .AppendCertsFromPEM (pemBytes )
129137 }
130138
139+ if c .appID == "" {
140+ return errNoAppID .New ()
141+ }
131142 if c .appAccessKey == "" {
132143 return errNoAppAccessKey .New ()
133144 }
@@ -155,3 +166,8 @@ func (c *Config) Initialize(rootConfig source.Config) error {
155166
156167 return nil
157168}
169+
170+ // Flags returns the flags for the configuration.
171+ func (c * Config ) Flags () * pflag.FlagSet {
172+ return c .flags
173+ }
0 commit comments