github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/inspect/modules/list.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     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 inspect
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect"
    26  )
    27  
    28  type moduleList struct {
    29  	Modules []moduleEntry `json:"modules"`
    30  }
    31  
    32  type moduleEntry struct {
    33  	Name     string `json:"name"`
    34  	Path     string `json:"path"`
    35  	IsRemote bool   `json:"isRemote,omitempty"`
    36  	IsRoot   bool   `json:"isRoot,omitempty"`
    37  }
    38  
    39  func PrintModulesList(ctx context.Context, out io.Writer, opts inspect.Options) error {
    40  	formatter := inspect.OutputFormatter(out, opts.OutFormat)
    41  	cfgs, err := inspect.GetConfigSet(ctx, config.SkaffoldOptions{ConfigurationFile: opts.Filename, RepoCacheDir: opts.RepoCacheDir})
    42  	if err != nil {
    43  		formatter.WriteErr(err)
    44  		return err
    45  	}
    46  
    47  	l := &moduleList{Modules: []moduleEntry{}}
    48  	if err != nil {
    49  		formatter.WriteErr(err)
    50  		return err
    51  	}
    52  	for _, c := range cfgs {
    53  		if c.Metadata.Name != "" {
    54  			l.Modules = append(l.Modules, moduleEntry{Name: c.Metadata.Name, Path: c.SourceFile, IsRemote: c.IsRemote, IsRoot: c.IsRootConfig})
    55  		} else if opts.ModulesOptions.IncludeAll {
    56  			// if the `--all` flag is selected, include unnamed modules with the generated name `__config_<index>`
    57  			// we need a generated name to disambiguate between multiple unnamed modules in the same file.
    58  			l.Modules = append(l.Modules, moduleEntry{Name: fmt.Sprintf("__config_%d", c.SourceIndex), Path: c.SourceFile, IsRemote: c.IsRemote, IsRoot: c.IsRootConfig})
    59  		}
    60  	}
    61  	return formatter.Write(l)
    62  }