go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/appengine/impl/settings/settings.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package settings contains definition of global CIPD backend settings.
    16  package settings
    17  
    18  import (
    19  	"flag"
    20  
    21  	"go.chromium.org/luci/common/errors"
    22  
    23  	api "go.chromium.org/luci/cipd/api/cipd/v1"
    24  	"go.chromium.org/luci/cipd/appengine/impl/gs"
    25  )
    26  
    27  // Settings contain CIPD backend settings.
    28  type Settings struct {
    29  	// StorageGSPath is GS path in a form of /bucket/path to the root of the
    30  	// content-addressable storage area in Google Storage.
    31  	//
    32  	// The files will be stored as /storage_gs_path/hash_algo/hex_digest.
    33  	StorageGSPath string
    34  
    35  	// TempGSPath is GS path in a form of /bucket/path to the root of the storage
    36  	// area for pending uploads.
    37  	//
    38  	// It contains unverified files uploaded by clients before they pass the
    39  	// hash verification check and copied to the CAS storage area.
    40  	TempGSPath string
    41  }
    42  
    43  // Register registers settings as CLI flags.
    44  func (s *Settings) Register(f *flag.FlagSet) {
    45  	f.StringVar(
    46  		&s.StorageGSPath,
    47  		"cipd-storage-gs-path",
    48  		s.StorageGSPath,
    49  		"The root of the content-addressable storage area in Google Storage as a '/bucket/path' string.",
    50  	)
    51  	f.StringVar(
    52  		&s.TempGSPath,
    53  		"cipd-temp-gs-path",
    54  		s.TempGSPath,
    55  		"The root of the pending uploads storage area in Google Storage as a '/bucket/path' string.",
    56  	)
    57  }
    58  
    59  // Validate validates settings format.
    60  func (s *Settings) Validate() error {
    61  	if s.StorageGSPath == "" {
    62  		return errors.Reason("-cipd-storage-gs-path is required").Err()
    63  	}
    64  	if err := gs.ValidatePath(s.StorageGSPath); err != nil {
    65  		return errors.Annotate(err, "bad -cipd-storage-gs-path").Err()
    66  	}
    67  	if s.TempGSPath == "" {
    68  		return errors.Reason("-cipd-temp-gs-path is required").Err()
    69  	}
    70  	if err := gs.ValidatePath(s.TempGSPath); err != nil {
    71  		return errors.Annotate(err, "bad -cipd-temp-gs-path").Err()
    72  	}
    73  	return nil
    74  }
    75  
    76  // ObjectPath constructs a path to the object in the Google Storage, starting
    77  // from StorageGSPath root.
    78  func (s *Settings) ObjectPath(obj *api.ObjectRef) string {
    79  	return s.StorageGSPath + "/" + obj.HashAlgo.String() + "/" + obj.HexDigest
    80  }