~ubuntu-branches/ubuntu/raring/joyent-mdata-client/raring-updates

« back to all changes in this revision

Viewing changes to mdata_get.c

  • Committer: Package Import Robot
  • Author(s): Ben Howard
  • Date: 2013-11-07 08:14:52 UTC
  • Revision ID: package-import@ubuntu.com-20131107081452-z0kajfx8fpi5prgg
Tags: upstream-0.0.1
ImportĀ upstreamĀ versionĀ 0.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2013, Joyent, Inc.
 
3
 * See LICENSE file for copyright and license details.
 
4
 */
 
5
 
 
6
#include <stdlib.h>
 
7
#include <stdio.h>
 
8
#include <sys/types.h>
 
9
#include <sys/stat.h>
 
10
#include <fcntl.h>
 
11
#include <unistd.h>
 
12
#include <err.h>
 
13
#include <errno.h>
 
14
#include <string.h>
 
15
#include <strings.h>
 
16
 
 
17
#include "common.h"
 
18
#include "dynstr.h"
 
19
#include "plat.h"
 
20
#include "proto.h"
 
21
 
 
22
typedef enum mdata_exit_codes {
 
23
        MDEC_SUCCESS = 0,
 
24
        MDEC_NOTFOUND = 1,
 
25
        MDEC_ERROR = 2,
 
26
        MDEC_USAGE_ERROR = 3,
 
27
        MDEC_TRY_AGAIN = 10
 
28
} mdata_exit_codes_t;
 
29
 
 
30
static char *keyname;
 
31
 
 
32
static int
 
33
print_response(mdata_response_t mdr, string_t *data)
 
34
{
 
35
        const char *cstr = dynstr_cstr(data);
 
36
        size_t len = dynstr_len(data);
 
37
 
 
38
        switch (mdr) {
 
39
        case MDR_SUCCESS:
 
40
                fprintf(stdout, "%s", cstr);
 
41
                if (len < 1 || cstr[len - 1] != '\n')
 
42
                        fprintf(stdout, "\n");
 
43
                return (MDEC_SUCCESS);
 
44
        case MDR_NOTFOUND:
 
45
                fprintf(stderr, "No metadata for '%s'\n", keyname);
 
46
                return (MDEC_NOTFOUND);
 
47
        case MDR_UNKNOWN:
 
48
                fprintf(stderr, "Error getting metadata for key '%s': %s\n",
 
49
                    keyname, cstr);
 
50
                return (MDEC_ERROR);
 
51
        case MDR_INVALID_COMMAND:
 
52
                fprintf(stderr, "ERROR: host does not support GET\n");
 
53
                return (MDEC_ERROR);
 
54
        default:
 
55
                ABORT("print_response: UNKNOWN RESPONSE\n");
 
56
                return (MDEC_ERROR);
 
57
        }
 
58
}
 
59
 
 
60
int
 
61
main(int argc, char **argv)
 
62
{
 
63
        mdata_proto_t *mdp;
 
64
        mdata_response_t mdr;
 
65
        string_t *data;
 
66
        char *errmsg = NULL;
 
67
 
 
68
        if (argc < 2) {
 
69
                errx(MDEC_USAGE_ERROR, "Usage: %s <keyname>", argv[0]);
 
70
        }
 
71
 
 
72
        if (proto_init(&mdp, &errmsg) != 0) {
 
73
                fprintf(stderr, "ERROR: could not initialise protocol: %s\n",
 
74
                    errmsg);
 
75
                return (MDEC_ERROR);
 
76
        }
 
77
 
 
78
        keyname = strdup(argv[1]);
 
79
 
 
80
        if (proto_execute(mdp, "GET", keyname, &mdr, &data) != 0) {
 
81
                fprintf(stderr, "ERROR: could not execute GET\n");
 
82
                return (MDEC_ERROR);
 
83
        }
 
84
 
 
85
        return (print_response(mdr, data));
 
86
}