github.com/ledgerwatch/erigon-lib@v1.0.0/common/cli.go (about)

     1  /*
     2     Copyright 2021 Erigon contributors
     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 common
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"os/signal"
    23  	"strings"
    24  	"syscall"
    25  
    26  	"github.com/ledgerwatch/log/v3"
    27  )
    28  
    29  func RootContext() (context.Context, context.CancelFunc) {
    30  	ctx, cancel := context.WithCancel(context.Background())
    31  	go func() {
    32  		defer cancel()
    33  
    34  		ch := make(chan os.Signal, 1)
    35  		defer close(ch)
    36  
    37  		signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
    38  		defer signal.Stop(ch)
    39  
    40  		select {
    41  		case sig := <-ch:
    42  			log.Info("Got interrupt, shutting down...", "sig", sig)
    43  		case <-ctx.Done():
    44  		}
    45  	}()
    46  	return ctx, cancel
    47  }
    48  
    49  func CliString2Array(input string) []string {
    50  	l := strings.Split(input, ",")
    51  	res := make([]string, 0, len(l))
    52  	for _, r := range l {
    53  		if r = strings.TrimSpace(r); r != "" {
    54  			res = append(res, r)
    55  		}
    56  	}
    57  	return res
    58  }