Make install idempotent to jsonnetfile.json

This commit is contained in:
Frederic Branczyk 2018-04-24 17:43:27 +01:00
parent cb59934d1d
commit 5233278a99
No known key found for this signature in database
GPG key ID: 7741A52782A90069
2 changed files with 28 additions and 33 deletions

View file

@ -132,7 +132,8 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
name = repo name = repo
} }
} }
m.Dependencies = append(m.Dependencies, spec.Dependency{
newDep := spec.Dependency{
Name: name, Name: name,
Source: spec.Source{ Source: spec.Source{
GitSource: &spec.GitSource{ GitSource: &spec.GitSource{
@ -141,26 +142,25 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
}, },
}, },
Version: version, Version: version,
}) }
oldDeps := m.Dependencies
newDeps := []spec.Dependency{}
oldDepReplaced := false
for _, d := range oldDeps {
if d.Name == newDep.Name {
newDeps = append(newDeps, newDep)
oldDepReplaced = true
} else { } else {
m.Dependencies = append(m.Dependencies, spec.Dependency{ newDeps = append(newDeps, d)
Name: args[0],
Source: spec.Source{
GitSource: &spec.GitSource{
Remote: args[1],
Subdir: args[2],
},
},
})
} }
} }
{ if !oldDepReplaced {
b, err := json.MarshalIndent(m, "", " ") newDeps = append(newDeps, newDep)
if err != nil { }
return errors.Wrap(err, "failed to encode jsonnet file")
m.Dependencies = newDeps
} }
fmt.Println(string(b))
} }
srcPath := filepath.Join(cfg.JsonnetHome) srcPath := filepath.Join(cfg.JsonnetHome)
@ -180,7 +180,7 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create tmp dir") return errors.Wrap(err, "failed to create tmp dir")
} }
//defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
subdir := "" subdir := ""
var p pkg.Interface var p pkg.Interface
@ -211,15 +211,15 @@ func RunSubcommand(ctx context.Context, cfg config, subcommand string, args []st
return errors.Wrap(err, "failed to create parent path") return errors.Wrap(err, "failed to create parent path")
} }
// The path is encoded in the dir, so no need to do anything if it err = os.RemoveAll(destPath)
// already exists. if err != nil {
if _, err := os.Stat(destPath); os.IsNotExist(err) { return errors.Wrap(err, "failed to clean previous destination path")
err := os.Rename(path.Join(tmpDir, subdir), destPath) }
err = os.Rename(path.Join(tmpDir, subdir), destPath)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to move package") return errors.Wrap(err, "failed to move package")
} }
} }
}
b, err := json.MarshalIndent(m, "", " ") b, err := json.MarshalIndent(m, "", " ")
if err != nil { if err != nil {

View file

@ -20,7 +20,6 @@ type JsonnetFile struct {
type Source struct { type Source struct {
GitSource *GitSource `json:"git"` GitSource *GitSource `json:"git"`
GitHubSource *GitHubSource `json:"github"`
} }
type GitSource struct { type GitSource struct {
@ -28,10 +27,6 @@ type GitSource struct {
Subdir string `json:"subdir"` Subdir string `json:"subdir"`
} }
type GitHubSource struct {
Repo string `json:"repo"`
}
type Dependency struct { type Dependency struct {
Name string `json:"name"` Name string `json:"name"`
Source Source `json:"source"` Source Source `json:"source"`