/
sync.go
102 lines (88 loc) · 3.59 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"log"
"path/filepath"
"github.com/spf13/cobra"
"go.opentelemetry.io/build-tools/multimod/internal/sync"
)
var (
otherVersioningFile string
otherRepoRoot string
allModuleSetsSync bool
moduleSetNamesSync []string
skipGoModTidySync bool
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "Syncs the versions of a repo's dependencies",
Long: `Updates version numbers of module sets from another repo:
- Checks that the working tree is clean.
- Switches to a new branch called prerelease_<module set name>_<new version>.
- Updates module versions in all go.mod files.
- Attempts to call go mod tidy on the files.
- Adds and commits changes to Git branch`,
PreRun: func(cmd *cobra.Command, args []string) {
if allModuleSetsSync {
// do not require module set names if operating on all module sets
if err := cmd.Flags().SetAnnotation(
"module-set-names",
cobra.BashCompOneRequiredFlag,
[]string{"false"},
); err != nil {
log.Fatalf("could not set module-set-names flag as not required flag: %v", err)
}
}
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Using versioning file", versioningFile)
if otherVersioningFile == "" {
otherVersioningFile = filepath.Join(otherRepoRoot,
fmt.Sprintf("%v.%v", defaultVersionsConfigName, defaultVersionsConfigType))
}
sync.Run(versioningFile, otherVersioningFile, otherRepoRoot, moduleSetNamesSync, allModuleSetsSync, skipGoModTidySync)
},
}
func init() {
// Plain log output, no timestamps.
log.SetFlags(0)
rootCmd.AddCommand(syncCmd)
syncCmd.Flags().StringVarP(&otherRepoRoot, "other-repo-root", "o", "",
"File path of other repository root whose modules' versions need to be updated.")
if err := syncCmd.MarkFlagRequired("other-repo-root"); err != nil {
log.Fatalf("could not mark other-repo-root flag as required: %v", err)
}
syncCmd.Flags().StringVar(&otherVersioningFile, "other-versioning-file", "",
"Path to other versioning file that contains all module set versions to sync. "+
"If unspecified, defaults to versions.yaml in the other Git repo root.")
syncCmd.Flags().BoolVarP(&allModuleSetsSync, "all-module-sets", "a", false,
"Specify this flag to update versions of modules in all sets listed in the versioning file.",
)
syncCmd.Flags().StringSliceVarP(&moduleSetNamesSync, "module-set-names", "m", nil,
"Names of module set whose version is being changed. "+
"Each name be listed in the module set versioning YAML. "+
"To specify multiple module sets, specify set names as comma-separated values."+
"For example: --module-set-names=\"mod-set-1,mod-set-2\"",
)
if err := syncCmd.MarkFlagRequired("module-set-names"); err != nil {
log.Fatalf("could not mark module-set-names flag as required: %v", err)
}
syncCmd.Flags().BoolVarP(&skipGoModTidySync, "skip-go-mod-tidy", "s", false,
"Specify this flag to skip invoking `go mod tidy`. "+
"To be used for debugging purposes. Should not be skipped during actual release.",
)
}