Example #1
0
// NewOptions makes some *Options with defaults!
func NewOptions() *Options {
	cwd, _ := os.Getwd()
	cwd = env.Get("TRAVIS_BUILD_DIR", cwd)

	targetPaths := env.ExpandSlice(env.Slice("ARTIFACTS_TARGET_PATHS", ":", []string{}))
	if len(targetPaths) == 0 {
		targetPaths = DefaultTargetPaths
	}

	return &Options{
		AccessKey: env.Cascade([]string{
			"ARTIFACTS_KEY",
			"ARTIFACTS_AWS_ACCESS_KEY",
			"AWS_ACCESS_KEY_ID",
			"AWS_ACCESS_KEY",
		}, ""),
		SecretKey: env.Cascade([]string{
			"ARTIFACTS_SECRET",
			"ARTIFACTS_AWS_SECRET_KEY",
			"AWS_SECRET_ACCESS_KEY",
			"AWS_SECRET_KEY",
		}, ""),
		BucketName: strings.TrimSpace(env.Cascade([]string{
			"ARTIFACTS_BUCKET",
			"ARTIFACTS_S3_BUCKET",
		}, "")),
		CacheControl: strings.TrimSpace(env.Get("ARTIFACTS_CACHE_CONTROL", DefaultCacheControl)),
		Perm:         s3.ACL(env.Get("ARTIFACTS_PERMISSIONS", DefaultPerm)),

		RepoSlug:    DefaultRepoSlug,
		BuildNumber: DefaultBuildNumber,
		BuildID:     DefaultBuildID,
		JobNumber:   DefaultJobNumber,
		JobID:       DefaultJobID,

		Concurrency: env.Uint("ARTIFACTS_CONCURRENCY", DefaultConcurrency),
		MaxSize:     env.UintSize("ARTIFACTS_MAX_SIZE", DefaultMaxSize),
		Paths:       env.ExpandSlice(env.Slice("ARTIFACTS_PATHS", ":", DefaultPaths)),
		Provider:    env.Get("ARTIFACTS_UPLOAD_PROVIDER", DefaultUploadProvider),
		Retries:     env.Uint("ARTIFACTS_RETRIES", DefaultRetries),
		TargetPaths: targetPaths,
		WorkingDir:  cwd,

		ArtifactsSaveHost:  env.Get("ARTIFACTS_SAVE_HOST", ""),
		ArtifactsAuthToken: env.Get("ARTIFACTS_AUTH_TOKEN", ""),
	}
}
func (opts *Options) reset() {
	s := reflect.ValueOf(opts).Elem()
	t := s.Type()

	for i := 0; i < s.NumField(); i++ {
		f := s.Field(i)
		if !f.CanSet() {
			continue
		}

		tf := t.Field(i)
		dflt := os.ExpandEnv(optsMaps["default"][tf.Name])
		envKeys := strings.Split(optsMaps["env"][tf.Name], ",")
		value, envVar := env.CascadeMatch(envKeys, dflt)

		if value == "" {
			continue
		}

		if envVar == "" {
			envVar = envKeys[0]
		}

		value = os.ExpandEnv(value)

		k := f.Kind()
		switch k {
		case reflect.String:
			f.SetString(value)
		case reflect.Uint64:
			uintVal, err := strconv.ParseUint(dflt, 10, 64)
			if err != nil {
				fmt.Fprintf(os.Stderr, "ERROR: %v", err)
			} else {
				f.SetUint(env.Uint(envVar, uintVal))
			}
		case reflect.Slice:
			sliceValue := env.Slice(envVar, ":", strings.Split(":", dflt))
			f.Set(reflect.ValueOf(sliceValue))
		default:
			panic(fmt.Sprintf("unknown kind wat: %v", k))
		}
	}
}