github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/manifest/data.go (about)

     1  package manifest
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/fastly/cli/pkg/env"
     7  )
     8  
     9  // Data holds global-ish manifest data from manifest files, and flag sources.
    10  // It has methods to give each parameter to the components that need it,
    11  // including the place the parameter came from, which is a requirement.
    12  //
    13  // If the same parameter is defined in multiple places, it is resolved according
    14  // to the following priority order: the manifest file (lowest priority) and then
    15  // environment variables (where applicable), and explicit flags (highest priority).
    16  type Data struct {
    17  	File File
    18  	Flag Flag
    19  }
    20  
    21  // Authors yields an Authors.
    22  func (d *Data) Authors() ([]string, Source) {
    23  	if len(d.Flag.Authors) > 0 {
    24  		return d.Flag.Authors, SourceFlag
    25  	}
    26  
    27  	if len(d.File.Authors) > 0 {
    28  		return d.File.Authors, SourceFile
    29  	}
    30  
    31  	return []string{}, SourceUndefined
    32  }
    33  
    34  // Description yields a Description.
    35  func (d *Data) Description() (string, Source) {
    36  	if d.File.Description != "" {
    37  		return d.File.Description, SourceFile
    38  	}
    39  
    40  	return "", SourceUndefined
    41  }
    42  
    43  // Name yields a Name.
    44  func (d *Data) Name() (string, Source) {
    45  	if d.File.Name != "" {
    46  		return d.File.Name, SourceFile
    47  	}
    48  
    49  	return "", SourceUndefined
    50  }
    51  
    52  // ServiceID yields a ServiceID.
    53  func (d *Data) ServiceID() (string, Source) {
    54  	if d.Flag.ServiceID != "" {
    55  		return d.Flag.ServiceID, SourceFlag
    56  	}
    57  
    58  	if sid := os.Getenv(env.ServiceID); sid != "" {
    59  		return sid, SourceEnv
    60  	}
    61  
    62  	if d.File.ServiceID != "" {
    63  		return d.File.ServiceID, SourceFile
    64  	}
    65  
    66  	return "", SourceUndefined
    67  }