github.com/mborho/rem@v0.17.0/cli.go (about)

     1  // rem - A tool to remember things on the command line.
     2  // Copyright (C) 2015 Martin Borho (martin@borho.net)
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"strings"
    23  )
    24  
    25  var (
    26  	globalFlag *bool
    27  	helpFlag   *bool
    28  	addFlag    *bool
    29  	tagFlag    *string
    30  	printFlag  *bool
    31  	filter     *string
    32  )
    33  
    34  func init() {
    35  	// read flags
    36  	globalFlag = flag.Bool("g", false, "use global rem file")
    37  	helpFlag = flag.Bool("h", false, "show this help")
    38  	addFlag = flag.Bool("a", false, "add a command")
    39  	tagFlag = flag.String("t", "", "tag for command")
    40  	printFlag = flag.Bool("p", false, "print command before executing")
    41  	filter = flag.String("f", "", "List commands by regexp filter.")
    42  }
    43  
    44  // Reads command line arguments and runs rem.
    45  func run(remfile string) error {
    46  	flag.Parse()
    47  
    48  	// build rem type
    49  	rem := &Rem{
    50  		File: File{
    51  			filename: remfile,
    52  			global:   *globalFlag,
    53  		},
    54  		printBeforeExec: *printFlag,
    55  	}
    56  	rem.read()
    57  
    58  	// check flags and run specific method.
    59  	var err error
    60  	var index int
    61  
    62  	remCmd := flag.Arg(0)
    63  	switch {
    64  	case (remCmd == "help" || *helpFlag == true):
    65  		fmt.Println(help)
    66  	case remCmd == "here":
    67  		err = rem.createLocalFile()
    68  	case remCmd == "clear":
    69  		err = rem.clearFile()
    70  	case (remCmd == "add" || *addFlag == true):
    71  		startIndex := 1
    72  		if *addFlag == true {
    73  			startIndex = 0
    74  		}
    75  		toAdd := strings.TrimSpace(strings.Join(flag.Args()[startIndex:], " "))
    76  		if toAdd == "" {
    77  			// read line from stdIn
    78  			toAdd = rem.readFromStdIn()
    79  		}
    80  		err = rem.appendLine(toAdd, *tagFlag)
    81  	case (remCmd == "filter"):
    82  		err = rem.filterLines(strings.Join(flag.Args()[1:], " "))
    83  	case *filter != "":
    84  		err = rem.filterLines(*filter)
    85  	case remCmd == "edit":
    86  		if index, err = toInt(flag.Arg(1)); err == nil {
    87  			err = rem.editIndex(index)
    88  		} else {
    89  			err = rem.editTag(flag.Arg(1))
    90  		}
    91  	case remCmd == "rm":
    92  		if index, err = toInt(flag.Arg(1)); err == nil {
    93  			err = rem.removeLine(index)
    94  		}
    95  	case remCmd == "echo":
    96  		if flag.Arg(1) != "" {
    97  			if index, err = toInt(flag.Arg(1)); err == nil {
    98  				err = rem.printLine(index)
    99  			} else {
   100  				err = rem.printTag(flag.Arg(1))
   101  			}
   102  		}
   103  	case remCmd != "":
   104  		if index, err = toInt(remCmd); err == nil {
   105  			err = rem.executeIndex(index)
   106  		} else {
   107  			err = rem.executeTag(remCmd)
   108  		}
   109  	default:
   110  		rem.printAllLines()
   111  		if len(rem.lines) == 0 {
   112  			// show help if nothing was found
   113  			fmt.Println(help)
   114  		}
   115  	}
   116  	return err
   117  }