github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/deploy/util/logfile.go (about)

     1  /*
     2  Copyright 2020 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 util
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/logfile"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
    25  )
    26  
    27  // TimeFormat is used to name log files generated by deploy step
    28  const TimeFormat = "2006-01-02_15-04-05"
    29  
    30  type Muted interface {
    31  	MuteStatusCheck() bool
    32  	MuteDeploy() bool
    33  }
    34  
    35  // WithLogFile returns a file to write the deploy output to, and a function to be executed after the deploy step is complete.
    36  func WithLogFile(filename string, out io.Writer, muted Muted) (io.Writer, func(), error) {
    37  	if !muted.MuteDeploy() {
    38  		return out, func() {}, nil
    39  	}
    40  
    41  	file, err := logfile.Create("deploy", filename)
    42  	if err != nil {
    43  		return out, func() {}, fmt.Errorf("unable to create log file for deploy step: %w", err)
    44  	}
    45  
    46  	output.Default.Fprintln(out, "Starting deploy...")
    47  	fmt.Fprintln(out, "- writing logs to", file.Name())
    48  
    49  	// After the deploy finishes, close the log file.
    50  	return file, func() {
    51  		file.Close()
    52  	}, err
    53  }
    54  
    55  // WithStatusCheckLogFile returns a file to write the status-check output to, and a function to be executed after the status-check step is complete.
    56  func WithStatusCheckLogFile(filename string, out io.Writer, muted Muted) (io.Writer, func(), error) {
    57  	if !muted.MuteStatusCheck() {
    58  		return out, func() {}, nil
    59  	}
    60  
    61  	file, err := logfile.Create("status-check", filename)
    62  	if err != nil {
    63  		return out, func() {}, fmt.Errorf("unable to create log file for deploy step: %w", err)
    64  	}
    65  
    66  	output.Default.Fprintln(out, "Waiting for deployments to stabilize...")
    67  	fmt.Fprintln(out, "- writing logs to", file.Name())
    68  
    69  	// After the status-check finishes, close the log file.
    70  	return file, func() {
    71  		file.Close()
    72  	}, err
    73  }