-
Notifications
You must be signed in to change notification settings - Fork 243
/
infocmd.go
106 lines (94 loc) · 3.25 KB
/
infocmd.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
103
104
105
106
/*
* Copyright 2019 The Kythe Authors. All rights reserved.
*
* 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 infocmd provides the kzip command for obtaining info about a kzip archive.
package infocmd // import "kythe.io/kythe/go/platform/tools/kzip/infocmd"
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"strings"
"kythe.io/kythe/go/platform/kzip"
"kythe.io/kythe/go/platform/kzip/info"
"kythe.io/kythe/go/platform/vfs"
"kythe.io/kythe/go/util/cmdutil"
"github.com/google/subcommands"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
)
type infoCommand struct {
cmdutil.Info
input string
writeFormat string
readConcurrency int
}
// New creates a new subcommand for obtaining info on a kzip file.
func New() subcommands.Command {
return &infoCommand{
Info: cmdutil.NewInfo("info", "info on single kzip archive", "--input path"),
}
}
// SetFlags implements the subcommands interface and provides command-specific flags
// for the info command.
func (c *infoCommand) SetFlags(fs *flag.FlagSet) {
fs.StringVar(&c.input, "input", "", "Path for input kzip file (required)")
fs.StringVar(&c.writeFormat, "write_format", "json", "Output format, can be 'json' or 'proto'")
fs.IntVar(&c.readConcurrency, "read_concurrency", runtime.NumCPU(), "Max concurrency of reading compilation units from the kzip. Defaults to the number of cpu cores.")
}
// Execute implements the subcommands interface and gathers info from the requested file.
func (c *infoCommand) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if c.input == "" {
return c.Fail("Required --input path missing")
}
f, err := vfs.Open(ctx, c.input)
if err != nil {
return c.Fail("Opening archive: %v", err)
}
defer f.Close()
c.writeFormat = strings.ToLower(c.writeFormat)
if c.writeFormat != "json" && c.writeFormat != "proto" {
return c.Fail("Invalid --write_format. Can be 'json' or 'proto'.")
}
s, err := vfs.Stat(ctx, c.input)
if err != nil {
return c.Fail("Couldn't stat kzip file: %v", err)
}
kzipInfo, err := info.KzipInfo(f, s.Size(), kzip.ReadConcurrency(c.readConcurrency))
if err != nil {
return c.Fail("Scanning kzip: %v", err)
}
var rec []byte
switch c.writeFormat {
case "json":
m := protojson.MarshalOptions{UseProtoNames: true}
rec, err = m.Marshal(kzipInfo)
if err != nil {
return c.Fail("Marshaling json: %v", err)
}
case "proto":
rec, err = prototext.Marshal(kzipInfo)
if err != nil {
return c.Fail("Marshaling text: %v", err)
}
}
// Add a new line to the output after we write the kzip info.
defer func() { fmt.Println() }()
if _, err := os.Stdout.Write(rec); err != nil {
return c.Fail("Writing unit: %v", err)
}
return subcommands.ExitSuccess
}