diff --git a/cmd/mirror.go b/cmd/mirror.go index b8f6fa1a4a..290ca4ea90 100644 --- a/cmd/mirror.go +++ b/cmd/mirror.go @@ -230,7 +230,7 @@ func newMirrorModifyCmd() *cobra.Command { yanked := false cmd := &cobra.Command{ - Use: "modify [flags]", + Use: "modify [:version] [flags]", Long: "modify component attributes (hidden, standalone, yanked)", RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { @@ -253,7 +253,8 @@ func newMirrorModifyCmd() *cobra.Command { return err } - m, err := env.V1Repository().FetchComponentManifest(args[0]) + comp, ver := environment.ParseCompVersion(args[0]) + m, err := env.V1Repository().FetchComponentManifest(comp) if err != nil { return err } @@ -261,7 +262,7 @@ func newMirrorModifyCmd() *cobra.Command { if endpoint == "" { endpoint = environment.Mirror() } - e := remote.NewEditor(endpoint, args[0]).WithDesc(desc) + e := remote.NewEditor(endpoint, comp).WithDesc(desc).WithVersion(ver.String()) flagSet := set.NewStringSet() cmd.Flags().Visit(func(f *pflag.Flag) { flagSet.Insert(f.Name) diff --git a/pkg/repository/remote/modify.go b/pkg/repository/remote/modify.go index 2ff75e1e0c..ff3fbdad5c 100644 --- a/pkg/repository/remote/modify.go +++ b/pkg/repository/remote/modify.go @@ -18,11 +18,14 @@ import ( "time" "github.com/google/uuid" + "github.com/juju/errors" + "github.com/pingcap/tiup/pkg/repository/v0manifest" "github.com/pingcap/tiup/pkg/repository/v1manifest" ) // Editor defines the methods to modify a component attrs type Editor interface { + WithVersion(version string) Editor WithDesc(desc string) Editor Standalone(bool) Editor Hide(bool) Editor @@ -33,6 +36,7 @@ type Editor interface { type editor struct { endpoint string component string + version string description string options map[string]bool } @@ -46,6 +50,12 @@ func NewEditor(endpoint, component string) Editor { } } +// WithVersion set version field +func (e *editor) WithVersion(version string) Editor { + e.version = version + return e +} + // WithDesc set description field func (e *editor) WithDesc(desc string) Editor { e.description = desc @@ -81,5 +91,20 @@ func (e *editor) Sign(key *v1manifest.KeyInfo, m *v1manifest.Component) error { sid := uuid.New().String() url := fmt.Sprintf("%s/api/v1/component/%s/%s", e.endpoint, sid, e.component) + if e.version != "" { + // Only support modify yanked field for specified versiion + for p := range m.Platforms { + if v0manifest.Version(e.version).IsNightly() { + return errors.New("nightly version can't be yanked") + } + vi, ok := m.Platforms[p][e.version] + if !ok { + continue + } + vi.Yanked = e.options["yanked"] + m.Platforms[p][e.version] = vi + } + return signAndSend(url, m, key, nil) + } return signAndSend(url, m, key, e.options) } diff --git a/pkg/repository/v1manifest/manifest.go b/pkg/repository/v1manifest/manifest.go index 35355169fa..0f02d1cf53 100644 --- a/pkg/repository/v1manifest/manifest.go +++ b/pkg/repository/v1manifest/manifest.go @@ -79,7 +79,7 @@ var ManifestsConfig = map[string]ty{ ManifestTypeComponent: { Filename: "", Versioned: true, - Expire: time.Hour * 24 * 365, // 1y + Expire: time.Hour * 24 * 365 * 5, // 5y Threshold: 1, }, ManifestTypeSnapshot: { diff --git a/pkg/repository/v1manifest/types_test.go b/pkg/repository/v1manifest/types_test.go new file mode 100644 index 0000000000..dd2a7cc6bc --- /dev/null +++ b/pkg/repository/v1manifest/types_test.go @@ -0,0 +1,85 @@ +// Copyright 2020 PingCAP, Inc. +// +// 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, +// See the License for the specific language governing permissions and +// limitations under the License. + +package v1manifest + +import ( + "testing" + + "github.com/alecthomas/assert" +) + +func TestComponentList(t *testing.T) { + manifest := &Index{ + Components: map[string]ComponentItem{ + "comp1": ComponentItem{}, + "comp2": ComponentItem{Yanked: true}, + }, + } + + list := manifest.ComponentList() + assert.Equal(t, len(list), 1) + _, ok := list["comp1"] + assert.True(t, ok) + + list = manifest.ComponentListWithYanked() + assert.Equal(t, len(list), 2) + _, ok = list["comp1"] + assert.True(t, ok) + _, ok = list["comp2"] + assert.True(t, ok) +} + +func TestVersionList(t *testing.T) { + manifest := &Component{ + Platforms: map[string]map[string]VersionItem{ + "linux/amd64": { + "v1.0.0": {Entry: "test"}, + "v1.1.1": {Entry: "test", Yanked: true}, + }, + "any/any": { + "v1.0.0": {Entry: "test"}, + "v1.1.1": {Entry: "test", Yanked: true}, + }, + }, + } + + versions := manifest.VersionList("linux/amd64") + assert.Equal(t, len(versions), 1) + _, ok := versions["v1.0.0"] + assert.True(t, ok) + + versions = manifest.VersionListWithYanked("linux/amd64") + assert.Equal(t, len(versions), 2) + _, ok = versions["v1.0.0"] + assert.True(t, ok) + _, ok = versions["v1.1.1"] + assert.True(t, ok) + + versions = manifest.VersionList("windows/amd64") + assert.Equal(t, len(versions), 1) + _, ok = versions["v1.0.0"] + assert.True(t, ok) + + manifest = &Component{ + Platforms: map[string]map[string]VersionItem{ + "linux/amd64": { + "v1.0.0": {Entry: "test"}, + "v1.1.1": {Entry: "test", Yanked: true}, + }, + }, + } + + versions = manifest.VersionList("windows/amd64") + assert.Equal(t, len(versions), 0) +} diff --git a/tests/tiup/tiup.toml b/tests/tiup/tiup.toml new file mode 100644 index 0000000000..598fc52376 --- /dev/null +++ b/tests/tiup/tiup.toml @@ -0,0 +1 @@ +mirror = "https://tiup-mirrors.pingcap.com"