aboutsummaryrefslogblamecommitdiffstats
path: root/src/actionhelper.c
blob: 58990240d675db4d572d189ef03d68db12cd7023 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                                               







                                                                                
                                                                                

                   
                         
                       


                     
 











                                






                                                          
                                                              
                      






                                                                  
                       
                                                      

                                                    
              
   
           

 

                                       
                                                             







                               

                                              


















                                                                        


                                       
                









                                          
                                
          
                                



                                      





                    










                                                                        











                                                                                                  
                      
                                     







                              
 









                                                              






































                                                                                    


                                         
                   
                      
                  
              
               




                        



                                              



                                              


























                                                         












                                                       
            
                        






                             





                                                  
        

                                                

                                                     

                  


                            
                                                            





                                                                           

                  










                             



                   

                                 
                                    


   

                                
                                      

           
                                              

 

                                
                                      

           



                                                   

 

                                 

           

 

                                 

           

 
                                   
                 
                                                                        

           
             

           
                       

 

                                 
   

 
                                    

                           
   

 





























































                                                                                                     

                                                                                      























                                                                                      
 
 
                           


















                                                                                             


                



















                                                                                   
/* actionhelper.c - helper routines for ck actions --------------------*- C -*-
 *
 * This file is part of ck, the config keeper
 *
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2018  Anastasis Grammenos
 * GPLv3 (see LICENCE for the full notice)
 *
 * -------------------------------------------------------------------------- */
#include <libgen.h>

#include "actionhelper.h"
#include "confparser.h"
#include "ckerrlog.h"

ERRLOG(action);

char add_err[STR_M] = "";

int add_err_message(char *err) {
  if (!str_is_empty(add_err)) {
    if (err) {
      strcpy(err, add_err);
    }
    return 1;
  }
  return 0;
}

void link_config(const AddOpt *opt, const char* newPath) {
  printf("Linking %s -> %s\n", newPath, opt->confPath);
  if (util_symlink_file(newPath, opt->confPath) != 0) {
    strcpy(add_err, "Could not link file.");
  }
}

int move_config(const AddOpt *opt, char *progDir, char *ret) {
  char newPath[STR_L];
  char *tmp = strdup(opt->confPath);
  str_join_dirname_with_basename(newPath, progDir, basename(tmp));
  free(tmp);
  if (util_file_exists(newPath, NULL)) {
    strcpy(add_err, "File already exists");\
    return -1;
  }
  strcpy(ret, newPath);
  printf("Moving %s -> %s\n", opt->confPath, newPath);
  if (util_move_file(opt->confPath, newPath) != 0) {
    strcpy(add_err, "Could not move file.");
    return -1;
  }
  return 0;
}

AddOpt add_make_options(cklist *args) {
  list_rewind(args);
  /* since we are here, the first two arguments must exist */
  AddOpt addOpt = {
    .progName = list_get(args),
    .secret = 0,
    .prime = 0,
    .err = ADD_NO_ERR
  };

  list_next(args);
  if (!util_is_file_rw(list_get(args))
      || !util_is_file_link(list_get(args))) {
    addOpt.err = ADD_ERR_WRONG_CONFIG;
    return addOpt;
  }
  realpath(list_get(args), addOpt.confPath);

  while (list_next(args)) {
    if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) {
      addOpt.secret = 1;
    } else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) {
      addOpt.prime = 1;
    } else {
      addOpt.err = ADD_ERR_WRONG_FLAGS;
      return addOpt;
    }
  }
  list_rewind(args);
  return addOpt;
}

DelOpt del_make_options(cklist *args) {
  list_rewind(args);
  DelOpt delOpt = {
    .arg = NULL,
    .isConf = 0,
    .err = DEL_NO_ERR
  };

  if (strcmp(list_get(args), "-c") == 0) {
    delOpt.isConf = 1;
    if (!list_next(args)) {
      delOpt.err = DEL_ERR_WRONG_ARGS;
      return delOpt;
    }
    delOpt.arg = list_get(args);
  } else {
    delOpt.arg = list_get(args);
    if (list_next(args)) {
      delOpt.err = DEL_ERR_WRONG_ARGS;
      return delOpt;
    }
  }

  list_rewind(args);
  return delOpt;
}

void add_print_opts(AddOpt *opt) {
  printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath);
  if (opt->prime && opt->secret) {
    printf("Options:\tsecret, primary\n");
  } else if (opt->prime) {
    printf("Options:\tprimary\n");
  } else if (opt->secret) {
    printf("Options:\tsecret\n");
  }
}

void get_or_make_program_dir(const AddOpt *opt, const Conf *conf, char *ret) {
  char tmp[STR_L] = "";
  str_join_dirname_with_basename(tmp, opt->secret ? conf->scrt_dir : conf->vc_dir, opt->progName);
  if (!util_file_exists(tmp, NULL)) {
    util_mkdir(tmp);
  }
  strcpy(ret, tmp);
}

void add_make_link(const AddOpt *opt, const Conf *conf) {
  char progDir[STR_L];
  get_or_make_program_dir(opt, conf, progDir);
  char newPath[STR_L];
  move_config(opt, progDir, newPath);
  if (add_err_message(NULL)) {
    return;
  }
  link_config(opt, newPath);
  if (add_err_message(NULL)) {
    return;
  }
}

void edit_print_suggested_configs(DB *db, const char *pName) {
  char name[STR_M] = "";
  strcat(name, pName);
  strcat(name, ":");
  cklist *paths = list_make_and_add(name);
  get_program_paths(db, paths, pName, 1, 0);
  list_print(paths);
  list_free(paths);
}

int init_create_config_file(UserOpt *opt) {
  char absVCdir[STR_L];
  if (!util_file_exists(list_get_at(opt->args, 0), absVCdir)) {
    ERR("Version control directory: %s does not exist.", list_get_at(opt->args, 0));
    return 1;
  }

  char absSRdir[STR_L];
  if (!util_file_exists(list_get_at(opt->args, 1), absSRdir)) {
    ERR("Secret directory: %s does not exist.", list_get_at(opt->args, 1));
    return 1;
  }

  if (!util_file_exists(opt->confDir, NULL)) {
    util_mkdir(opt->confDir);
  }

  char confName[STR_L];
  make_config_name(confName, opt->confDir);
  FILE *f;
  if ((f = fopen(confName, "w")) == NULL) {
    return 1;
  }

  char tmp[200];
  strcpy(tmp, "version_control_dir = ");
  strcat(tmp, absVCdir);
  strcat(tmp, "\n");
  fputs(tmp, f);

  strcpy(tmp, "secret_dir = ");
  strcat(tmp, absSRdir);
  strcat(tmp, "\n");
  fputs(tmp, f);

  fclose(f);
  return 0;
}

ListOpt list_make_options(cklist *args) {
  list_rewind(args);
  ListOpt listOpt = {
    ._lt = LT_TREE,
    ._lst = LST_PLAIN,
    .pName = NULL,
    .attr = 0,
    .bName = 0,
    .err = 0
  };

  if (list_size(args)) {
    do {
      if (strcmp(list_get(args), "-a") == 0) {
        listOpt.attr = 1;
        continue;
      }
      if (strcmp(list_get(args), "-b") == 0) {
        listOpt.bName = 1;
        continue;
      }
      if (strcmp(list_get(args), "-t") == 0) {
	if (!list_next(args)) {
	  listOpt.err = 1;
	  break;
	}
	if (strcmp(list_get(args), "plain") == 0) {
	  listOpt._lst = LST_PLAIN;
	}
	else if (strcmp(list_get(args), "lisp") == 0) {
	  listOpt._lst = LST_LISP;
	}
	else if (strcmp(list_get(args), "python") == 0) {
	  listOpt._lst = LST_PYTHON;
	}
	else {
	  listOpt.err = 1;
	}
      }
      else if (strcmp(list_get(args), "paths") == 0) {
	listOpt._lt = LT_PATH;
      }
      else if (strcmp(list_get(args), "programs") == 0) {
	listOpt._lt = LT_PROGRAM;
      }
      else if (strcmp(list_get(args), "tree") == 0) {
        listOpt._lt = LT_TREE;
      }
      else if (strcmp(list_get(args), "ckconf") == 0) {
        listOpt._lt = LT_CKCONF;
      }
      else if (strcmp(list_get(args), "-p") == 0) {
        if (list_next(args)) {
          listOpt._lt = LT_PROG_CONFS;
          listOpt.pName = list_get(args);
        }
        else {
          listOpt.err = 1;
          break;
        }
      }
      else {
        listOpt.err = 1;
      }
    } while(list_next(args));
  }
  list_rewind(args);
  return listOpt;
}

int restore_make_links(cklist *from, cklist *to) {
  list_rewind(from);
  list_rewind(to);
  if (list_size(from) > 0
      && list_size(to) > 0
      && list_size(from) == list_size(to)) {
    do {
      if (util_file_exists(list_get(to), NULL)
          || !util_is_file_link(list_get(to))) {
        ERR("File %s already exists.", list_get(to));
        sERR("No links were created.");
        return -1;
      }
    } while (list_next(to));
    list_rewind(to);
    while (1) {
      if (util_symlink_file(list_get(from), list_get(to))) {
        ERR("FATAL could not link %s -> %s", list_get(from), list_get(to));
        sERR("Process stopping.");
        return -1;
      }
      hLOG("Linking: %s -> %s", list_get(from), list_get(to));
      if (util_own_grp_copy(list_get(to), list_get(from))) {
        return -1;
      }
      if (!list_next(from)) {
        break;
      }
      if (!list_next(to)) {
        break;
      }
    }
  }
  return 0;
}

/*****************/
/* PRINT RESULTS */
/*****************/

void print_INIT_result(int err) {
  if (!err) {
    HELP("Initialized empty ckdb.");
  }
}

void print_ADD_result(int err) {
  if (!err) {
    HELP("ckdb updated succesfully.");
    return;
  }
  sERR("Could not complete add transaction.");
}

void print_DEL_result(int err) {
  if (!err) {
    HELP("ckdb updated succesfully.");
    return;
  }
  if (err == -1) {
    sERR("Could not complete delete transaction.");
    return;
  }
}

void print_EDIT_result(int err) {
  if (!err) {
    return;
  }
}

void print_LIST_result(int err) {
  if (!err) {
    return;
  }
}

void print_SEARCH_result(int err) {
  if (err == 2) {
    ERR("No grep avaliable. Please make sure you have grep installed.");
    return;
    }
  if (!err) {
    return;
  }
  ERR("Wrong search.");
}

void print_HELP_result(int err) {
  if (!err) {
  }
}

void print_RESTORE_result(int err) {
  if (err == -1) {
    sERR("Restore failed.")
  }
}

void print_INIT_help() {
  ckhelp("Initialize the database and create the file that holds");
  ckhelp("the paths where configurations will be stored.");
  ckhelp("Namely the version control and the secret directories.\n");
  ckhelp("It takes two arguments:");
  ckhelp("  `version_control_dir`: the path to the version control directory");
  ckhelp("  `secret_dir`: the path to the secret directory");
  report_help();
}

void print_ADD_help() {
  ckhelp("Add a config for ck to keep track of.\n");
  ckhelp("The file will be stored to the desired directory and will");
  ckhelp("be linked back to it's original position.\n");
  ckhelp("It takes the following arguments:");
  ckhelp("  `program_name`: the name of the config's program");
  ckhelp("  `path`: path to the configuration (can be relative)\n");
  ckhelp("It also takes the flags:");
  ckhelp("  `-p`: to mark a configuration as primary (so it will open with edit by default)");
  ckhelp("  `-s`: to mark a configuration as secret (to end up in the secret dir)\n");
  ckhelp("Note:\nThere can be only one primary config per program.");
  ckhelp("It is advised to use your distribution's package name for the program_name.");
  ckhelp("The arguments have to be in the order shown here (name -> path -> flags)");
  report_help();
}

void print_DEL_help() {
  ckhelp("Delete a program/config from ck.\n");
  ckhelp("This will not interfere with any files or links, it will");
  ckhelp("just remove the program/config from the ck database.\n");
  ckhelp("To completely remove a program/config you have to manually");
  ckhelp("erase it from the folder it is stored. You can do it either before");
  ckhelp("or after running this command.\n");
  ckhelp("It takes one or two arguments:");
  ckhelp("  `program_name`: will delete all configs under that program.");
  ckhelp("  `-c path`: will delete the config specified by the path.\n");
  ckhelp("Note:\nThe path that follows the -c option has to be the path shown");
  ckhelp("when you list the configurations of a program. (ck help list for more)");
  report_help();
}

void print_EDIT_help() {
  ckhelp("Edit a config tracked by ck.\n");
  ckhelp("It takes one or two arguments:");
  ckhelp("  `program_name`: will open the primary config of that program (if set)");
  ckhelp("  `config_name`: will open the specified config\n");
  ckhelp("Note:\nIf a program has only one config, edit will open it even if it's not the primary.");
  ckhelp("Edit will use the $EDITOR and if it's not set it falls back to nano.");
  ckhelp("If there is ambiguity ck will list the available config names");
  ckhelp("for the program specified.");
  report_help();
}

void print_LIST_help() {
  ckhelp("List various stuff ck knows about.\n");
  ckhelp("List can work in a few different ways:");
  ckhelp("  `tree`: list all the programs and paths in a tree like structure.");
  ckhelp("  `paths`: list all the configs ck keeps track.");
  ckhelp("  `programs`: list all the programs ck keeps track.");
  ckhelp("  `-p program_name`: list only the configs of the specified program.");
  ckhelp("  `ckconf`: list the ck configuration values.\n");
  ckhelp("There are also some flags:");
  ckhelp("  `-a`: add the attributes next to the config paths (secret,primary,root)");
  ckhelp("  `-b`: print the config basenames instead of the full path");
  ckhelp("  `-t`: change the list type. (does not work with tree or ckconf)");
  ckhelp("      `plain`: default plain listing");
  ckhelp("      `python`: enclose the list in [ , ]");
  ckhelp("      `lisp`: enclose the list in ( , )");
  report_help();
}

void print_SEARCH_help() {
  ckhelp("Search through the configs ck keeps track of.\n");
  ckhelp("Search performs a grep with the supplied term to each");
  ckhelp("file ck keeps track and prints the results.\n");
  ckhelp("The exact grep command used is `grep -H -n`\n");
  ckhelp("It takes one argument:");
  ckhelp("  `search_term`\n");
  ckhelp("Note:\nIf you want to search for a phrase enclose it in \'\' or \"\"");
  report_help();
}

void print_HELP_help() {
  ckhelp("Get help for ck actions.\n");
  ckhelp("It takes one argument:");
  ckhelp("  `action`\n");
  ckhelp("Note:\nRunning ck without arguments will print all the available actions.");
  report_help();
}

void print_RESTORE_help() {
  ckhelp("Restore links.\n");
  ckhelp("Given a working ck instance (ckdb + ckrc + directories in ckrc with configs)");
  ckhelp("restore shall recreate the links from the config directories in ckrc");
  ckhelp("back to their corresponding position when added in ck.\n");
  ckhelp("It is useful for copying your configs to a new linux installation");
  ckhelp("or restoring deleted links.\n");
  ckhelp("It can either restore a specific program or all of them:");
  ckhelp("  `-p progName`: restores progName.");
  ckhelp("  `all`: restores everything.\n");
  ckhelp("Note:\nIf ck tracks configs that are owned by root, simply running");
  ckhelp("`ck restore ...` will fail due to permissions. To remedy this, ck will alter the");
  ckhelp("owner and group of a link to match the one in the ckrc directories.");
  ckhelp("Thus, running `sudo ck -c /home/myuser/.ck restore ..` will restore");
  ckhelp("the root user's links as it should and the user links will have");
  ckhelp("the user as the owner instead of the root.\n");
  ckhelp("ck checks that the configs exist and that the location for the link");
  ckhelp("is avaliable before making any links. However, in the even that symlink");
  ckhelp("fails for some other reason, the process will stop as is. Make sure you");
  ckhelp("take care of the already created links, if that's the case.");
  report_help();
}

void print_conf_help(void) {
  ckhelp("Set a different ck configuration directory.\n");
  ckhelp("This has to be passed before any action or action argument");
  ckhelp("and after verbose (if set).\n");
  ckhelp("Usage:");
  ckhelp("  ck -c /path/to/conf ...");
  ckhelp("  ck config /path/to/conf ...\n");
  ckhelp("ck will use the database and config file in the path supplied");
  ckhelp("to perform any actions.\n");
  ckhelp("Tip:\nYou can alias `ck -c /path/to/conf` and use the new alias");
  ckhelp("to organise a different set of configurations, or some custom scrpits.");
  report_help();
}

void print_verbose_help(void) {
  ckhelp("WIP\n");
  ckhelp("Currently passing the verbose flag, prints a log of what ck is doing");
  ckhelp("during excecution of an action.");
  report_help();
}