Coder Social home page Coder Social logo

"If X exists" about script HOT 8 CLOSED

bitfield avatar bitfield commented on August 24, 2024
"If X exists"

from script.

Comments (8)

bitfield avatar bitfield commented on August 24, 2024

I'd love some suggestions for what this might look like!

from script.

frioux avatar frioux commented on August 24, 2024

Wouldn't it be something like:

func IsFile(path string) (bool, error) {
   s, err := os.Stat(path)
   if err != nil {
       if os.IsNotExist(err) {
          return false, nil
       }
       return false, err
   }

   return !s.IsDir(), nil
}

from script.

bitfield avatar bitfield commented on August 24, 2024

Sorry—my fault for not being clear. Yes, the implementation would probably look something like what you quoted, though we need to think about whether we should consider both files and directories to 'exist'.

The more difficult question is about how it would work within script pipelines. For example, would it be something like:

script.IfExists("/srv/foo/config.txt").Exec("/usr/bin/configure-service")

What do you think? A use case would be helpful here. Have you ever written a script which has logic based on whether or not a file exists?

from script.

frioux avatar frioux commented on August 24, 2024

Here are some examples I have on hand (in shell:)

if [ -f $x ]; then
     cat $x
else
     ls $x --color
fi

if [ -f "$HOME/.goobook_auth.json" ]; then
   goobook query '' >> "$TMP"
   < $TMP grep -v '^ ' | sponge $TMP
fi

if [ -f "$HOME/.dbic.json" ]; then
   export DBIC_TRACE_PROFILE="$HOME/.dbic.json"
fi

if [[ ! -f ~/.dotfilecheck ]]; then
   echo $(($EPOCHSECONDS+60*60*24*7)) > ~/.dotfilecheck
fi

from script.

bitfield avatar bitfield commented on August 24, 2024

Great! Would you mind writing out one of these examples using script, as you would like to see it?

from script.

bitfield avatar bitfield commented on August 24, 2024

@frioux (or anyone else) do you have any comments on the proposed design?

from script.

frioux avatar frioux commented on August 24, 2024

from script.

bitfield avatar bitfield commented on August 24, 2024

Well, then it's even more helpful of you to pop in and make suggestions! I appreciate it.

Here's the final result:

// IfExists tests whether the specified file exists, and returns a pipe whose
// error status reflects the result. If the file doesn't exist, the pipe's error
// status will be set, and if the file does exist, the pipe will have no error
// status.
func IfExists(filename string) *Pipe {
	_, err := os.Stat(filename)
	if err != nil {
		return NewPipe().WithError(err)
	}
	return NewPipe()
}

from script.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.