1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package commands
18
19import (
20	"io"
21	"io/ioutil"
22	"os"
23	"text/tabwriter"
24
25	pb "github.com/containerd/continuity/proto"
26	"github.com/golang/protobuf/proto"
27	"github.com/spf13/cobra"
28)
29
30var (
31	MainCmd = &cobra.Command{
32		Use:   "continuity <command>",
33		Short: "A transport-agnostic filesytem metadata tool.",
34	}
35
36	// usageTemplate is nearly identical to the default template without the
37	// automatic addition of flags. Instead, Command.Use is used unmodified.
38	usageTemplate = `Usage:{{if .Runnable}}
39  {{.UseLine}}{{end}}{{if .HasSubCommands}}
40  {{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
41
42Aliases:
43  {{.NameAndAliases}}
44{{end}}{{if .HasExample}}
45
46Examples:
47{{ .Example }}{{end}}{{ if .HasAvailableSubCommands}}
48
49Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
50  {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}}
51
52Flags:
53{{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasInheritedFlags}}
54
55Global Flags:
56{{.InheritedFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
57
58Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
59  {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}
60
61Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
62`
63)
64
65func init() {
66	MainCmd.AddCommand(BuildCmd)
67	MainCmd.AddCommand(VerifyCmd)
68	MainCmd.AddCommand(ApplyCmd)
69	MainCmd.AddCommand(LSCmd)
70	MainCmd.AddCommand(StatsCmd)
71	MainCmd.AddCommand(DumpCmd)
72	if MountCmd != nil {
73		MainCmd.AddCommand(MountCmd)
74	}
75	MainCmd.SetUsageTemplate(usageTemplate)
76}
77
78// readManifestFile reads the manifest from the given path. This should
79// probably be provided by the continuity library.
80func readManifestFile(path string) (*pb.Manifest, error) {
81	p, err := ioutil.ReadFile(path)
82	if err != nil {
83		return nil, err
84	}
85
86	var bm pb.Manifest
87
88	if err := proto.Unmarshal(p, &bm); err != nil {
89		return nil, err
90	}
91
92	return &bm, nil
93}
94
95// newTabwriter provides a common tabwriter with defaults.
96func newTabwriter(w io.Writer) *tabwriter.Writer {
97	return tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
98}
99