From b67ab4e608bce3a483fe25c4ef557da475a7468e Mon Sep 17 00:00:00 2001 From: Matthew Johnson Date: Thu, 3 May 2018 14:14:22 -0700 Subject: [PATCH] Add mixer repo set-url capability 'mixer repo set-url' allows the user to set the url of a RPM repo to the url they provide. This is helpful when users want to build mixes off a mirror of upstream Clear Linux instead of the CDN when the CDN is slow. mixer repo set-url clear Signed-off-by: Matthew Johnson --- builder/builder.go | 37 ++++++++++++++++++++++++++++++++----- mixer/cmd/repos.go | 27 ++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/builder/builder.go b/builder/builder.go index 7a0d856..ddffeab 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -1663,14 +1663,14 @@ gpgcheck=0 priority=1 ` -// AddRepo adds and enables a repo configuration named repoInfo[0] pointing at -// URL repoInfo[1]. It calls b.NewDNFConfIfNeeded() to create the DNF config if it +// AddRepo adds and enables a repo configuration named pointing at +// URL . It calls b.NewDNFConfIfNeeded() to create the DNF config if it // does not exist and performs a check to see if the repo passed has already // been configured. -func (b *Builder) AddRepo(repoInfo []string) error { +func (b *Builder) AddRepo(name, url string) error { repo := dnfRepoConf{ - RepoName: repoInfo[0], - RepoURL: repoInfo[1], + RepoName: name, + RepoURL: url, } if err := b.NewDNFConfIfNeeded(); err != nil { @@ -1708,6 +1708,33 @@ func (b *Builder) AddRepo(repoInfo []string) error { return nil } +// SetURLRepo sets the URL for the repo to . If does not exist it is +// created. +func (b *Builder) SetURLRepo(name, url string) error { + if err := b.NewDNFConfIfNeeded(); err != nil { + return err + } + + DNFConf, err := ini.Load(b.Config.Builder.DNFConf) + if err != nil { + return err + } + + s, err := DNFConf.GetSection(name) + if err != nil { + // the section doesn't exist, just add a new one + return b.AddRepo(name, url) + } + + k, err := s.GetKey("baseurl") + if err != nil { + return err + } + + k.SetValue(url) + return DNFConf.SaveTo(b.Config.Builder.DNFConf) +} + // RemoveRepo removes a configured repo if it exists in the DNF configuration. // This will fail if a DNF conf has not yet been generated. func (b *Builder) RemoveRepo(name string) error { diff --git a/mixer/cmd/repos.go b/mixer/cmd/repos.go index 5e3cf9e..15c81aa 100644 --- a/mixer/cmd/repos.go +++ b/mixer/cmd/repos.go @@ -57,11 +57,19 @@ var initRepoCmd = &cobra.Command{ Run: runInitRepo, } +var setURLRepoCmd = &cobra.Command{ + Use: "set-url ", + Short: "Sets the URL for repo to ", + Long: `Sets the URL, for repo to . If does not exist the repo will be added to the configuration.`, + Run: runSetURLRepo, +} + var repoCmds = []*cobra.Command{ addRepoCmd, removeRepoCmd, listReposCmd, initRepoCmd, + setURLRepoCmd, } func init() { @@ -82,7 +90,7 @@ func runAddRepo(cmd *cobra.Command, args []string) { fail(err) } - err = b.AddRepo(args) + err = b.AddRepo(args[0], args[1]) if err != nil { fail(err) } @@ -128,3 +136,20 @@ func runInitRepo(cmd *cobra.Command, args []string) { fail(err) } } + +func runSetURLRepo(cmd *cobra.Command, args []string) { + if len(args) != 2 { + fail(errors.New("set-url requires two arguments: ")) + } + + b, err := builder.NewFromConfig(config) + if err != nil { + fail(err) + } + + err = b.SetURLRepo(args[0], args[1]) + if err != nil { + fail(err) + } + fmt.Printf("Set %s baseurl to %s.\n", args[0], args[1]) +}