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  * <http://creativecommons.org/publicdomain/zero/1.0/>.
13  */
14 module libgit2_d.example.config;
15 
16 
17 private static import core.stdc.stdio;
18 private static import libgit2_d.config;
19 private static import libgit2_d.errors;
20 private static import libgit2_d.repository;
21 private static import libgit2_d.types;
22 
23 package:
24 
25 nothrow @nogc
26 private int config_get(libgit2_d.types.git_config* cfg, const (char)* key)
27 
28 	in
29 	{
30 	}
31 
32 	do
33 	{
34 		libgit2_d.config.git_config_entry* entry;
35 		int error = libgit2_d.config.git_config_get_entry(&entry, cfg, key);
36 
37 		if (error < 0) {
38 			if (error != libgit2_d.errors.git_error_code.GIT_ENOTFOUND) {
39 				core.stdc.stdio.printf("Unable to get configuration: %s\n", libgit2_d.errors.git_error_last().message);
40 			}
41 
42 			return 1;
43 		}
44 
45 		core.stdc.stdio.puts(entry.value);
46 
47 		return 0;
48 	}
49 
50 nothrow @nogc
51 private int config_set(libgit2_d.types.git_config* cfg, const (char)* key, const (char)* value)
52 
53 	in
54 	{
55 	}
56 
57 	do
58 	{
59 		if (libgit2_d.config.git_config_set_string(cfg, key, value) < 0) {
60 			core.stdc.stdio.printf("Unable to set configuration: %s\n", libgit2_d.errors.git_error_last().message);
61 
62 			return 1;
63 		}
64 
65 		return 0;
66 	}
67 
68 extern (C)
69 nothrow @nogc
70 public int lg2_config(libgit2_d.types.git_repository* repo, int argc, char** argv)
71 
72 	in
73 	{
74 	}
75 
76 	do
77 	{
78 		libgit2_d.types.git_config* cfg;
79 		int error = libgit2_d.repository.git_repository_config(&cfg, repo);
80 
81 		if (error < 0) {
82 			core.stdc.stdio.printf("Unable to obtain repository config: %s\n", libgit2_d.errors.git_error_last().message);
83 
84 			return error;
85 		}
86 
87 		if (argc == 2) {
88 			error = .config_get(cfg, argv[1]);
89 		} else if (argc == 3) {
90 			error = .config_set(cfg, argv[1], argv[2]);
91 		} else {
92 			core.stdc.stdio.printf("USAGE: %s config <KEY> [<VALUE>]\n", argv[0]);
93 			error = 1;
94 		}
95 
96 		return error;
97 	}