github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/cli/exit/exit.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package exit
    12  
    13  import (
    14  	"fmt"
    15  	"os"
    16  
    17  	"github.com/cockroachdb/redact"
    18  )
    19  
    20  // Code represents an exit code.
    21  type Code struct {
    22  	code int
    23  }
    24  
    25  // String implements the fmt.Stringer interface.
    26  func (c Code) String() string { return fmt.Sprint(c.code) }
    27  
    28  // Format implements the fmt.Formatter interface.
    29  func (c Code) Format(s fmt.State, verb rune) {
    30  	_, f := redact.MakeFormat(s, verb)
    31  	fmt.Fprintf(s, f, c.code)
    32  }
    33  
    34  // SafeValue implements the redact.SafeValue interface.
    35  func (c Code) SafeValue() {}
    36  
    37  var _ redact.SafeValue = Code{}
    38  
    39  // WithCode terminates the process and sets its exit status code to
    40  // the provided code.
    41  func WithCode(code Code) {
    42  	os.Exit(code.code)
    43  }