kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/tools/kzip/mergecmd/flag.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 mergecmd
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  
    23  	"kythe.io/kythe/go/util/vnameutil"
    24  )
    25  
    26  // vnameRules is a path-valued flag used for loading VName mapping rules from a vnames.json file.
    27  type vnameRules struct {
    28  	filename string
    29  	vnameutil.Rules
    30  }
    31  
    32  // Set implements part of the flag.Value interface and will set a new filename for the flag.
    33  func (f *vnameRules) Set(s string) error {
    34  	f.filename = s
    35  	data, err := ioutil.ReadFile(f.filename)
    36  	if err != nil {
    37  		return fmt.Errorf("reading vname rules: %v", err)
    38  	}
    39  	f.Rules, err = vnameutil.ParseRules(data)
    40  	if err != nil {
    41  		return fmt.Errorf("reading vname rules: %v", err)
    42  	}
    43  	return nil
    44  }
    45  
    46  // String implements part of the flag.Value interface and returns a string-ish value for the flag.
    47  func (f *vnameRules) String() string {
    48  	return f.filename
    49  }
    50  
    51  // Get implements part of the flag.Getter interface.
    52  func (f *vnameRules) Get() any {
    53  	return f
    54  }