Skip to content

Commit

Permalink
Support template command
Browse files Browse the repository at this point in the history
To provide standard templates and detailed instructions in the template files

Close #1024
  • Loading branch information
lucklove committed Feb 24, 2021
1 parent d5ae317 commit 82cc921
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/cluster/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func init() {
newPushCmd(),
newTestCmd(), // hidden command for test internally
newTelemetryCmd(),
newTemplateCmd(),
)
}

Expand Down
64 changes: 64 additions & 0 deletions components/cluster/command/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2021 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 command

import (
"fmt"
"path"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/embed"
"github.com/spf13/cobra"
)

// TemplateOptions contains the options for print topology template.
type TemplateOptions struct {
Full bool // print full template
MultiDC bool // print template for deploying to multiple data center
}

func newTemplateCmd() *cobra.Command {
opt := TemplateOptions{}

cmd := &cobra.Command{
Use: "template",
Short: "Print topology template",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
if opt.Full && opt.MultiDC {
return errors.New("at most one of 'full' and 'multi-dc' can be specified")
}
name := "minimal.yaml"
if opt.Full {
name = "topology.example.yaml"
} else if opt.MultiDC {
name = "multi-dc.yaml"
}

fp := path.Join("templates", "examples", name)
tpl, err := embed.ReadFile(fp)
if err != nil {
return err
}

fmt.Println(string(tpl))
return nil
},
}

cmd.Flags().BoolVar(&opt.Full, "full", false, "Print the full topology template for TiDB cluster.")
cmd.Flags().BoolVar(&opt.MultiDC, "multi-dc", false, "Print template for deploying to multiple data center.")

return cmd
}
1 change: 1 addition & 0 deletions components/dm/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ please backup your data before process.`,
newImportCmd(),
newEnableCmd(),
newDisableCmd(),
newTemplateCmd(),
)
}

Expand Down
56 changes: 56 additions & 0 deletions components/dm/command/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2021 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 command

import (
"fmt"
"path"

"github.com/pingcap/tiup/embed"
"github.com/spf13/cobra"
)

// TemplateOptions contains the options for print topology template.
type TemplateOptions struct {
Full bool // print full template
}

func newTemplateCmd() *cobra.Command {
opt := TemplateOptions{}

cmd := &cobra.Command{
Use: "template",
Short: "Print topology template",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
name := "minimal.yaml"
if opt.Full {
name = "topology.example.yaml"
}

fp := path.Join("templates", "examples", "dm", name)
tpl, err := embed.ReadFile(fp)
if err != nil {
return err
}

fmt.Println(string(tpl))
return nil
},
}

cmd.Flags().BoolVar(&opt.Full, "full", false, "Print the full topology template for DM cluster.")

return cmd
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions examples

0 comments on commit 82cc921

Please sign in to comment.