get.porter.sh/porter@v1.3.0/pkg/porter/logs.go (about)

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"get.porter.sh/porter/pkg/portercontext"
     9  )
    10  
    11  // LogsShowOptions represent options for an installation logs show command
    12  type LogsShowOptions struct {
    13  	installationOptions
    14  	RunID string
    15  }
    16  
    17  // Installation name passed to the command.
    18  func (o *LogsShowOptions) Installation() string {
    19  	return o.installationOptions.Name
    20  }
    21  
    22  // Validate validates the provided args, using the provided context,
    23  // setting attributes of LogsShowOptions as applicable
    24  func (o *LogsShowOptions) Validate(cxt *portercontext.Context) error {
    25  	if o.Name != "" && o.RunID != "" {
    26  		return errors.New("either --installation or --run should be specified, not both")
    27  	}
    28  
    29  	// Attempt to derive installation name from context
    30  	err := o.installationOptions.defaultBundleFiles(cxt)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	if o.File == "" && o.Name == "" && o.RunID == "" {
    36  		return errors.New("either --installation or --run is required")
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  // ShowInstallationLogs shows logs for an installation, according to the provided options.
    43  func (p *Porter) ShowInstallationLogs(ctx context.Context, opts *LogsShowOptions) error {
    44  	logs, ok, err := p.GetInstallationLogs(ctx, opts)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	if !ok {
    50  		return errors.New("no logs found")
    51  	}
    52  
    53  	fmt.Fprintln(p.Out, logs)
    54  	return nil
    55  }
    56  
    57  // GetInstallationLogs gets logs for an installation, according to the provided options
    58  func (p *Porter) GetInstallationLogs(ctx context.Context, opts *LogsShowOptions) (string, bool, error) {
    59  	err := p.applyDefaultOptions(ctx, &opts.installationOptions)
    60  	if err != nil {
    61  		return "", false, err
    62  	}
    63  	installation := opts.installationOptions.Name
    64  
    65  	if opts.RunID != "" {
    66  		return p.Installations.GetLogs(ctx, opts.RunID)
    67  	}
    68  
    69  	return p.Installations.GetLastLogs(ctx, opts.Namespace, installation)
    70  }