// repoAdd adds a chart repository. Code ported from:
// https://github.com/helm/helm/blob/v3.4.2/cmd/helm/repo_add.go
repoAdd adds a chart repository.
// repoAdd adds a chart repository . Code ported from : // https : // github . com / helm / helm / blob / v3 . 4 . 2 / cmd / helm / repo_add . go
func (o *repoAddOptions) repoAdd() error {
	err := os.MkdirAll(filepath.Dir(o.repoFile), os.ModePerm)
	if err != nil && !os.IsExist(err) {
		return trace.Wrap(err)
	}
	fileLock := flock.New(strings.Replace(o.repoFile, filepath.Ext(o.repoFile), ".lock", 1))
	lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	locked, err := fileLock.TryLockContext(lockCtx, time.Second)
	if err == nil && locked {
		defer fileLock.Unlock()
	}
	if err != nil {
		return trace.Wrap(err)
	}
	bytes, err := ioutil.ReadFile(o.repoFile)
	if err != nil && !os.IsNotExist(err) {
		return trace.Wrap(err)
	}
	var repoFile repo.File
	if err := yaml.Unmarshal(bytes, &repoFile); err != nil {
		return trace.Wrap(err)
	}
	entry := repo.Entry{
		Name:     o.name,
		URL:      o.url,
		Username: o.username,
		Password: o.password,
	}
	repository, err := repo.NewChartRepository(&entry, getter.All(cli.New()))
	if err != nil {
		return trace.Wrap(err)
	}
	if o.repoCache != "" {
		repository.CachePath = o.repoCache
	}
	if _, err := repository.DownloadIndexFile(); err != nil {
		return trace.Wrap(err, "looks like %q is not a valid chart repository or cannot be reached", o.url)
	}
	repoFile.Update(&entry)
	if err := repoFile.WriteFile(o.repoFile, defaults.SharedReadMask); err != nil {
		return trace.Wrap(err)
	}
	log.WithField("repo", o.name).Info("Repo has been added to your repositories.")
	return nil
}
func ( o * repoAddOptions ) repoAdd ( ) error { err := os . MkdirAll ( filepath . Dir ( o . repoFile ) , os . ModePerm ) if err != nil && ! os . IsExist ( err ) { return trace . Wrap ( err ) } fileLock := flock . New ( strings . Replace ( o . repoFile , filepath . Ext ( o . repoFile ) , " " , 1 ) ) lockCtx , cancel := context . WithTimeout ( context . Background ( ) , 30 * time . Second ) defer cancel ( ) locked , err := fileLock . TryLockContext ( lockCtx , time . Second ) if err == nil && locked { defer fileLock . Unlock ( ) } if err != nil { return trace . Wrap ( err ) } bytes , err := ioutil . ReadFile ( o . repoFile ) if err != nil && ! os . IsNotExist ( err ) { return trace . Wrap ( err ) } var repoFile repo . File if err := yaml . Unmarshal ( bytes , & repoFile ) ; err != nil { return trace . Wrap ( err ) } entry := repo . Entry { Name : o . name , URL : o . url , Username : o . username , Password : o . password , } repository , err := repo . NewChartRepository ( & entry , getter . All ( cli . New ( ) ) ) if err != nil { return trace . Wrap ( err ) } if o . repoCache != " " { repository . CachePath = o . repoCache } if _ , err := repository . DownloadIndexFile ( ) ; err != nil { return trace . Wrap ( err , " " , o . url ) } repoFile . Update ( & entry ) if err := repoFile . WriteFile ( o . repoFile , defaults . SharedReadMask ) ; err != nil { return trace . Wrap ( err ) } log . WithField ( " " , o . name ) . Info ( " " ) return nil }
repoAdd adds a chart repository.
repoAdd adds a chart repository .
// Ensure the file directory exists as it is required for file locking // Acquire a file lock for process synchronization