11package unpackerr
22
33import (
4+ "crypto/tls"
45 "fmt"
6+ "net/http"
57 "os"
68 "path/filepath"
79 "runtime"
@@ -15,6 +17,7 @@ import (
1517 homedir "github.com/mitchellh/go-homedir"
1618 "golift.io/cnfg"
1719 "golift.io/cnfgfile"
20+ "golift.io/starr"
1821)
1922
2023const (
@@ -31,11 +34,7 @@ func (u *Unpackerr) unmarshalConfig() (uint64, uint64, string, error) {
3134 def , cfl := configFileLocactions ()
3235 // Search for one, starting with the default.
3336 for _ , f = range append ([]string {u .Flags .ConfigFile }, cfl ... ) {
34- d , err := homedir .Expand (f )
35- if err == nil {
36- f = d
37- }
38-
37+ f = expandHomedir (f )
3938 if _ , err := os .Stat (f ); err == nil {
4039 break // found one, bail out.
4140 } // else { u.Print("rip:", err) }
@@ -193,12 +192,8 @@ func (u *Unpackerr) createConfigFile(file string) (string, error) {
193192 return "" , nil
194193 }
195194
196- file , err := homedir . Expand ( file )
195+ file , err := filepath . Abs ( expandHomedir ( file ) )
197196 if err != nil {
198- return "" , fmt .Errorf ("expanding home: %w" , err )
199- }
200-
201- if file , err = filepath .Abs (file ); err != nil {
202197 return "" , fmt .Errorf ("absolute file: %w" , err )
203198 }
204199
@@ -264,3 +259,67 @@ func isRunningInDocker() bool {
264259 _ , err := os .Stat ("/.dockerenv" )
265260 return err == nil
266261}
262+
263+ // expandHomedir expands a ~ to a homedir, or returns the original path in case of any error.
264+ func expandHomedir (filePath string ) string {
265+ expanded , err := homedir .Expand (filePath )
266+ if err != nil {
267+ return filePath
268+ }
269+
270+ return expanded
271+ }
272+
273+ func (u * Unpackerr ) validateApp (s * StarrConfig , app starr.App ) error {
274+ if s .URL == "" {
275+ u .Errorf ("Missing %s URL in one of your configurations, skipped and ignored." , app )
276+ return ErrInvalidURL // this error is not printed.
277+ }
278+
279+ if s .APIKey == "" {
280+ u .Errorf ("Missing %s API Key in one of your configurations, skipped and ignored." , app )
281+ return ErrInvalidURL // this error is not printed.
282+ }
283+
284+ if ! strings .HasPrefix (s .URL , "http://" ) && ! strings .HasPrefix (s .URL , "https://" ) {
285+ return fmt .Errorf ("%w: (%s) %s" , ErrInvalidURL , app , s .URL )
286+ }
287+
288+ if len (s .APIKey ) != apiKeyLength {
289+ return fmt .Errorf ("%s (%s) %w, your key length: %d" ,
290+ app , s .URL , ErrInvalidKey , len (s .APIKey ))
291+ }
292+
293+ if s .Timeout .Duration == 0 {
294+ s .Timeout .Duration = u .Timeout .Duration
295+ }
296+
297+ if s .DeleteDelay .Duration == 0 {
298+ s .DeleteDelay .Duration = u .DeleteDelay .Duration
299+ }
300+
301+ if s .Path != "" {
302+ s .Paths = append (s .Paths , s .Path )
303+ }
304+
305+ for idx , path := range s .Paths {
306+ s .Paths [idx ] = expandHomedir (path )
307+ }
308+
309+ if len (s .Paths ) == 0 {
310+ s .Paths = []string {defaultSavePath }
311+ }
312+
313+ if s .Protocols == "" {
314+ s .Protocols = defaultProtocol
315+ }
316+
317+ s .Config .Client = & http.Client {
318+ Timeout : s .Timeout .Duration ,
319+ Transport : & http.Transport {
320+ TLSClientConfig : & tls.Config {InsecureSkipVerify : ! s .ValidSSL }, //nolint:gosec
321+ },
322+ }
323+
324+ return nil
325+ }
0 commit comments