Run runs the Command.
()
| 138 | |
| 139 | // Run runs the Command. |
| 140 | func (u Command) Run() error { |
| 141 | if u.Output == nil { |
| 142 | u.Output = os.Stdout |
| 143 | } |
| 144 | |
| 145 | kptfile, err := kptfileutil.ReadFileStrict(u.Path) |
| 146 | if err != nil { |
| 147 | return errors.Errorf("unable to read package Kptfile: %v", err) |
| 148 | } |
| 149 | |
| 150 | // default arguments |
| 151 | if u.Repo == "" { |
| 152 | u.Repo = kptfile.Upstream.Git.Repo |
| 153 | } |
| 154 | if u.Ref == "" { |
| 155 | u.Ref = kptfile.Upstream.Git.Ref |
| 156 | } |
| 157 | |
| 158 | // require package is checked into git before trying to update it |
| 159 | g := gitutil.NewLocalGitRunner("./") |
| 160 | if err := g.Run("status", "-s", u.Path); err != nil { |
| 161 | return errors.Errorf( |
| 162 | "kpt packages must be checked into a git repo before they are updated: %v", err) |
| 163 | } |
| 164 | if strings.TrimSpace(g.Stdout.String()) != "" { |
| 165 | return errors.Errorf("must commit package %q to git before attempting to update", |
| 166 | u.Path) |
| 167 | } |
| 168 | |
| 169 | // update |
| 170 | updater, found := strategies[u.Strategy] |
| 171 | if !found { |
| 172 | return errors.Errorf("unrecognized update strategy %q", u.Strategy) |
| 173 | } |
| 174 | err = updater().Update(UpdateOptions{ |
| 175 | KptFile: kptfile, |
| 176 | ToRef: u.Ref, |
| 177 | ToRepo: u.Repo, |
| 178 | PackagePath: u.Path, |
| 179 | AbsPackagePath: u.FullPackagePath, |
| 180 | DryRun: u.DryRun, |
| 181 | Verbose: u.Verbose, |
| 182 | SimpleMessage: u.SimpleMessage, |
| 183 | Output: u.Output, |
| 184 | AutoSet: u.AutoSet, |
| 185 | }) |
| 186 | |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | |
| 191 | // perform auto-setters after the package is updated |
| 192 | a := setters.AutoSet{ |
| 193 | Writer: u.Output, |
| 194 | PackagePath: u.Path, |
| 195 | } |
| 196 | return a.PerformAutoSetters() |
| 197 | } |