forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
take slice of cmd/utils from upstream (ava-labs#1082)
* take slice of cmd/utils from upstream * add header
- Loading branch information
Showing
7 changed files
with
95 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2015 The go-ethereum Authors | ||
// This file is part of go-ethereum. | ||
// | ||
// go-ethereum is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// go-ethereum is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
// Package utils contains internal helper functions for go-ethereum commands. | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// Fatalf formats a message to standard error and exits the program. | ||
// The message is also printed to standard output if standard error | ||
// is redirected to a different file. | ||
func Fatalf(format string, args ...interface{}) { | ||
w := io.MultiWriter(os.Stdout, os.Stderr) | ||
if runtime.GOOS == "windows" { | ||
// The SameFile check below doesn't work on Windows. | ||
// stdout is unlikely to get redirected though, so just print there. | ||
w = os.Stdout | ||
} else { | ||
outf, _ := os.Stdout.Stat() | ||
errf, _ := os.Stderr.Stat() | ||
if outf != nil && errf != nil && os.SameFile(outf, errf) { | ||
w = os.Stderr | ||
} | ||
} | ||
fmt.Fprintf(w, "Fatal: "+format+"\n", args...) | ||
os.Exit(1) | ||
} | ||
|
||
// CheckExclusive verifies that only a single instance of the provided flags was | ||
// set by the user. Each flag might optionally be followed by a string type to | ||
// specialize it further. | ||
func CheckExclusive(ctx *cli.Context, args ...interface{}) { | ||
set := make([]string, 0, 1) | ||
for i := 0; i < len(args); i++ { | ||
// Make sure the next argument is a flag and skip if not set | ||
flag, ok := args[i].(cli.Flag) | ||
if !ok { | ||
panic(fmt.Sprintf("invalid argument, not cli.Flag type: %T", args[i])) | ||
} | ||
// Check if next arg extends current and expand its name if so | ||
name := flag.Names()[0] | ||
|
||
if i+1 < len(args) { | ||
switch option := args[i+1].(type) { | ||
case string: | ||
// Extended flag check, make sure value set doesn't conflict with passed in option | ||
if ctx.String(flag.Names()[0]) == option { | ||
name += "=" + option | ||
set = append(set, "--"+name) | ||
} | ||
// shift arguments and continue | ||
i++ | ||
continue | ||
|
||
case cli.Flag: | ||
default: | ||
panic(fmt.Sprintf("invalid argument, not cli.Flag or string extension: %T", args[i+1])) | ||
} | ||
} | ||
// Mark the flag if it's set | ||
if ctx.IsSet(flag.Names()[0]) { | ||
set = append(set, "--"+name) | ||
} | ||
} | ||
if len(set) > 1 { | ||
Fatalf("Flags %v can't be used at the same time", strings.Join(set, ", ")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.