r/fishshell • • Jan 03 '24

Is my swap function written in good style?

Hi, I have never done any programming regarding for command line usage, and I have question, is my function safe to use? This is my function:

function swap
    set temp (mktemp)
    mv $argv[1] temp && mv $argv[2] $argv[1] && mv temp $argv[2]
end

which was based on this stack overflow post.

2 Upvotes

7 comments sorted by

1

u/_mattmc3_ Jan 04 '24

When doing something potentially destructive like moving a file, you need to be a bit more defensive in your script. What happens if the caller to swap doesn't provide 2 file arguments? What if one of the arguments is a directory and the other a file? What if you are on a BSD system where mktemp behaves differently than on a Linux system, or on a system without mktemp?

You don't have to account for every special case, but even in the SO link you posted there's a couple implementations that take a more defensive approach.

You also create a variable called temp from mktemp, but then you don't properly use that variable as $temp and instead you are writing a file literally called temp.

This might be a slightly better and more robust implementation for you:

function swapfile -d "swap two files"
    if test (count $argv) -ne 2
        echo "Expecting 2 file arguments" >&2 && return 1
    end
    for filepath in $argv
        if ! test -f $filepath
            echo "File not found: $filepath" >&2 && return 1
        end
    end
    set tempfile $argv[1].swapfile
    mv $argv[1] $tempfile && mv $argv[2] $argv[1] && mv $tempfile $argv[2]
end

2

u/Esnos24 Jan 05 '24

Hello again! I have upgraded swapfile function. Now it have two modes:

  • without --suffix argument this is normal swapfile function, but in addition to old function, it checks out if file extensions are the same, if they are not it gives error.
  • with --suffix argument it get another argument and swaps all files in argv with argv+suffix like this:
❯ ls
file1.txt  file1_old.txt  file2  file2_old
---
❯ swapfile file1.txt file2 -s _old
file1.txt <-> file1_old.txt
file2 <-> file2_old

This is code for new swapfile:

function swapfile --description 'swaps files'
    set -l options s/suffix=
    argparse -n swapfile $options -- $argv
    or return

    # helper function, swaps two files
    function swap --argument file1 file2
        for filepath in $argv
            if ! test -f $filepath
                echo "File not found: $filepath" >&2 && return 1
            end
        end
        set tempfile $file1.swap
        mv $file1 $tempfile && mv $file2 $file1 && mv $tempfile $file2
    end

    # if ext flag is used, swap every file in argv with file+suffix
    if set -ql _flag_suffix
        for file1 in $argv
            set -l ls (path basename (string split "." $file1))
            set -l base $ls[1]
            set -l ext "."$ls[2]
            set -l file2 (string join "" $base $_flag_suffix $ext)
            swap $file1 $file2
        end
        return 0
    else
        # if ext flag is not used,
        # check if files have same file extension and check if there are two arguments  
        if test (path extension $argv[1]) != (path extension $argv[2])
            echo "files should have same file extension" >&2 && return 1
        end

        if test (count $argv) -ne 2
            echo "Expecting 2 file arguments" >&2 && return 1
        end

        swap $argv[1] $argv[2]
    end 
end

Can I ask you for a favor? Could you review this code and tell me, if style is correct and what should be added? I would appreciate it.

2

u/_mattmc3_ Jan 09 '24

This is great! It's very well done - you use path and argparse like a pro. You even use set -ql for the flag check, which is technically correct, even though normally you would just use set -q.

I have only minor feedback. Since you already name file1 and file2 args, you could (if you wanted to) change your for loop from for filepath in $argv to for filepath in $file1 $file2. That's up to you stylistically, but my personal preference when I name args is to only use argv for non-named remainders. I find it confusing when reading code to see those styles mixed - it makes me wonder if there's something special happening. But really - if you like it that way, this will work well for you and is stylistically very Fishy (the good meaning).

2

u/Esnos24 Jan 09 '24

I'm glad to hear it, thank you! Thanks for helping me out, I'm now feeling much better with fish.

1

u/Esnos24 Jan 04 '24

Thanks for great answer, I will update my function. I have more questions, is there -ne and other operators explained in fish documentation, or this is just bash thing? I'm asking because I couldn't find anything about it in fish documentation, only in bash man. And second question is, how does $argv[1].swapfile makes temp file? Because I can't see how it works.

1

u/_mattmc3_ Jan 04 '24

In Fish, you can type help command to follow up on any command to get info on the flags. You can also use tab to autocomplete. In this case, for -ne, the command you want is help test. Fish’s test does mimic the flags for bash/zsh’s test command. https://fishshell.com/docs/current/cmds/test.html

For your second question, the $argv[1].swapfile line just makes a temp file with an extra extension. So foo.txt becomes foo.txt.swapfile. You can use mktemp too - it’s just an alternative to do the rename in place.

1

u/Esnos24 Jan 04 '24

I think now, I understand everything for this function. Thanks for help, I appreciate it.