aboutsummaryrefslogtreecommitdiffstats
path: root/src/actionhelper.c
blob: c4c1f34a4e7d885f0c4be5e71da7533fa775797e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* 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 "actionhelper.h"

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

int copy_config(char *path);


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

void move_config(const AddOpt *opt, const Conf *conf, char *ret) {
  char newName[STR_M];
  str_make_ck_config_name(newName, opt->confPath, opt->progName);
  char newPath[STR_L];
  str_join_dirname_with_basename(newPath, opt->secret ? conf->SCRT_dir : conf->VC_dir, newName);
  printf("Moving %s -> %s\n", opt->confPath, newPath);
  strcpy(ret, newPath);
  if (util_move_file(opt->confPath, newPath) != 0) {
    strcpy(add_err, "Could not move file.");
  }
}

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

edit_rc
edit_get_config_or_suggestions(cklist *args, char *ret) {
  UNUSED(args);
  UNUSED(ret);
  return ERC_ERR;
}