kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/tools/kzip/metadatacmd/metadatacmd.go (about) 1 /* 2 * Copyright 2020 The Kythe Authors. All rights reserved. 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 17 // Package metadatacmd provides the kzip command for creating metadata kzips. 18 package metadatacmd // import "kythe.io/kythe/go/platform/tools/kzip/metadatacmd" 19 20 import ( 21 "context" 22 "flag" 23 "time" 24 25 "kythe.io/kythe/go/platform/kzip" 26 "kythe.io/kythe/go/platform/kzip/buildmetadata" 27 "kythe.io/kythe/go/platform/tools/kzip/flags" 28 "kythe.io/kythe/go/platform/vfs" 29 "kythe.io/kythe/go/util/cmdutil" 30 "kythe.io/kythe/go/util/log" 31 32 "github.com/google/subcommands" 33 ) 34 35 const ( 36 // GitDateFormat is the default date format used by `git log`. 37 GitDateFormat = "Mon Jan 2 15:04:05 2006 -0700" 38 ) 39 40 type metadataCommand struct { 41 cmdutil.Info 42 43 output string 44 corpus string 45 encoding flags.EncodingFlag 46 commitTimestamp string 47 timestampFormat string 48 } 49 50 // New creates a new subcommand for creating metadata kzip files. 51 func New() subcommands.Command { 52 return &metadataCommand{ 53 Info: cmdutil.NewInfo("create_metadata", "create a metadata kzip file, which records extra repository information", "--output path --commit_timestamp *"), 54 encoding: flags.EncodingFlag{Encoding: kzip.EncodingJSON}, 55 } 56 } 57 58 // SetFlags implements the subcommands interface and provides command-specific flags 59 // for merging kzip files. 60 func (c *metadataCommand) SetFlags(fs *flag.FlagSet) { 61 fs.StringVar(&c.output, "output", "", "Path to output kzip file") 62 fs.StringVar(&c.corpus, "corpus", "", "Corpus to set in the unit's VName") 63 fs.StringVar(&c.commitTimestamp, "commit_timestamp", "", "Timestamp when this version of the repository was checked in") 64 fs.StringVar(&c.timestampFormat, "timestamp_format", GitDateFormat, "Format of timestamp passed to --commit_timestamp. Defaults to git's default date format: 'Mon Jan 2 15:04:05 2006 -0700'") 65 fs.Var(&c.encoding, "encoding", "Encoding to use on output, one of JSON, PROTO, or ALL") 66 } 67 68 // Execute implements the subcommands interface and creates a metadata kzip file. 69 func (c *metadataCommand) Execute(ctx context.Context, fs *flag.FlagSet, _ ...any) (exitcode subcommands.ExitStatus) { 70 if c.output == "" { 71 return c.Fail("Required --output path missing") 72 } 73 if c.commitTimestamp == "" { 74 return c.Fail("Required --commit_timestamp missing") 75 } 76 if c.corpus == "" { 77 log.WarningContextf(ctx, "No --corpus provided") 78 } 79 80 t, err := time.Parse(c.timestampFormat, c.commitTimestamp) 81 if err != nil { 82 return c.Fail("Unable to parse timestamp: '%s'", c.commitTimestamp) 83 } 84 unit, err := buildmetadata.CreateMetadataUnit(c.corpus, t) 85 if err != nil { 86 return c.Fail("Error creating compilation unit: %v", err) 87 } 88 89 f, err := vfs.Create(ctx, c.output) 90 if err != nil { 91 return c.Fail("Creating output file: %v", err) 92 } 93 opt := kzip.WithEncoding(c.encoding.Encoding) 94 w, err := kzip.NewWriteCloser(f, opt) 95 if err != nil { 96 f.Close() 97 return c.Fail("Failed to close writer: %v", err) 98 } 99 if _, err := w.AddUnit(unit, nil); err != nil { 100 w.Close() 101 return c.Fail("Failed to add unit: %v", err) 102 } 103 if err := w.Close(); err != nil { 104 return c.Fail("Failed to close kzip: %v", err) 105 } 106 107 return subcommands.ExitSuccess 108 }