1 /*
2  * libgit2 "config" example - shows how to use the config API
3  *
4  * Written by the libgit2 contributors
5  *
6  * To the extent possible under law, the author(s) have dedicated all copyright
7  * and related and neighboring rights to this software to the public domain
8  * worldwide. This software is distributed without any warranty.
9  *
10  * You should have received a copy of the CC0 Public Domain Dedication along
11  * with this software. If not, see
12  * <https://creativecommons.org/publicdomain/zero/1.0/>.
13  */
14 /**
15  * License: $(LINK2 https://creativecommons.org/publicdomain/zero/1.0/, CC0 1.0 Universal)
16  */
17 module libgit2.example.config;
18 
19 
20 private static import core.stdc.stdio;
21 private static import libgit2.config;
22 private static import libgit2.errors;
23 private static import libgit2.repository;
24 private static import libgit2.types;
25 
26 nothrow @nogc
27 private int config_get(libgit2.types.git_config* cfg, const (char)* key)
28 
29 	in
30 	{
31 	}
32 
33 	do
34 	{
35 		libgit2.config.git_config_entry* entry;
36 		int error = libgit2.config.git_config_get_entry(&entry, cfg, key);
37 
38 		if (error < 0) {
39 			if (error != libgit2.errors.git_error_code.GIT_ENOTFOUND) {
40 				core.stdc.stdio.printf("Unable to get configuration: %s\n", libgit2.errors.git_error_last().message);
41 			}
42 
43 			return 1;
44 		}
45 
46 		core.stdc.stdio.puts(entry.value);
47 
48 		/* Free the git_config_entry after use with `git_config_entry_free()`. */
49 		libgit2.config.git_config_entry_free(entry);
50 
51 		return 0;
52 	}
53 
54 nothrow @nogc
55 private int config_set(libgit2.types.git_config* cfg, const (char)* key, const (char)* value)
56 
57 	in
58 	{
59 	}
60 
61 	do
62 	{
63 		if (libgit2.config.git_config_set_string(cfg, key, value) < 0) {
64 			core.stdc.stdio.printf("Unable to set configuration: %s\n", libgit2.errors.git_error_last().message);
65 
66 			return 1;
67 		}
68 
69 		return 0;
70 	}
71 
72 extern (C)
73 nothrow @nogc
74 public int lg2_config(libgit2.types.git_repository* repo, int argc, char** argv)
75 
76 	in
77 	{
78 	}
79 
80 	do
81 	{
82 		libgit2.types.git_config* cfg;
83 		int error = libgit2.repository.git_repository_config(&cfg, repo);
84 
85 		if (error < 0) {
86 			core.stdc.stdio.printf("Unable to obtain repository config: %s\n", libgit2.errors.git_error_last().message);
87 
88 			return error;
89 		}
90 
91 		if (argc == 2) {
92 			error = .config_get(cfg, argv[1]);
93 		} else if (argc == 3) {
94 			error = .config_set(cfg, argv[1], argv[2]);
95 		} else {
96 			core.stdc.stdio.printf("USAGE: %s config <KEY> [<VALUE>]\n", argv[0]);
97 			error = 1;
98 		}
99 
100 		/*
101 		 * The configuration file must be freed once it's no longer
102 		 * being used by the user.
103 		 */
104 		libgit2.config.git_config_free(cfg);
105 
106 		return error;
107 	}