github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/biz/impl/pipein.go (about)

     1  // Ultimate Provisioner: UP cmd
     2  // Copyright (c) 2019 Stephen Cheng and contributors
     3  
     4  /* This Source Code Form is subject to the terms of the Mozilla Public
     5   * License, v. 2.0. If a copy of the MPL was not distributed with this
     6   * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     7  
     8  package impl
     9  
    10  import (
    11  	"bufio"
    12  	u "github.com/upcmd/up/utils"
    13  	"io"
    14  	"os"
    15  )
    16  
    17  func Pipein() {
    18  	info, err := os.Stdin.Stat()
    19  	if err != nil {
    20  		u.LogErrorAndPanic("Pipe in error", err, "please double check you CLI pipe in syntax")
    21  	}
    22  
    23  	if info.Mode()&os.ModeCharDevice != 0 {
    24  		return
    25  	}
    26  
    27  	reader := bufio.NewReader(os.Stdin)
    28  	var pipeinchars []rune
    29  
    30  	for {
    31  		input, _, err := reader.ReadRune()
    32  		if err != nil && err == io.EOF {
    33  			break
    34  		}
    35  		pipeinchars = append(pipeinchars, input)
    36  	}
    37  
    38  	pipeinstr := string(pipeinchars)
    39  	UpRunTimeVars.Put(UP_RUNTIME_TASK_PIPE_IN_CONTENT, pipeinstr)
    40  }