aboutsummaryrefslogblamecommitdiffstats
path: root/src/actionhelper.c
blob: f0113bfc9eb1d508e3e76ad4fa78579ad6c94e0b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                                                

                   
                         


                     
 











                                






                                                          
                                                              
                      






                                                                  
                       
                                                      

                                                    
              
   
           

 

                                       
                                                             







                               

                                              


















                                                                        


                                       
                









                                          

                                       



                                      
                                





                    










                                                                        











                                                                                                  
                      
                                     







                              
 









                                                              




                                         
                  
              




                        



                                              


























                                                         












                                                       
            
                        










                             

                                 
                                    


   

                                
                                      

           
                                             

 

                                
                                     

           
                                               

 

                                 

           

 

                                 

           

 
                                   
                 
                                                                        

           
             

           
                       

 

                                 




                       
/* actionhelper.c - helper functions 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 "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);
    if (!util_is_file_rw(delOpt.arg)) {
      delOpt.err = DEL_ERR_WRONG_PATH;
      return delOpt;
    }
  } else {
    delOpt.arg = list_get(args);
  }

  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);
}

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

  if (list_size(args)) {
    do {
      if (strcmp(list_get(args), "-a") == 0) {
        listOpt.attr = 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;
}

/*****************/
/* 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;
  }
  ERR("Could not complete add transaction.");
}

void print_DEL_result(int err) {
  if (!err) {
    HELP("ckdb updated succesfully");
    return;
  }
  ERR("Could not complete delete transaction");
}

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) {
    printf("succes\n");
    return;
  }
  printf("failure\n");
}