Просмотр исходного кода

More wip in the vein of first time config scripts

Andy Baugh 9 лет назад
Родитель
Сommit
6724d8dd28
4 измененных файлов с 110 добавлено и 7 удалено
  1. 2 0
      TODO
  2. 71 0
      bin/configure-tCMS
  3. 26 7
      sys/admin/lib/configure.php
  4. 11 0
      t/admin-configure.t

+ 2 - 0
TODO

@@ -7,3 +7,5 @@ tCMS2 still left undone (will add to as I think of em)
 * tests for config writing
 * Un-Stubbify first time run wizard
 * Make config page in Admin work right
+
+*SINGLE USER INSTALL INFO TO MANUAL, MULTI-USER INSTALL INFO TO MANUAL

+ 71 - 0
bin/configure-tCMS

@@ -0,0 +1,71 @@
+#!/usr/bin/php
+<?php
+# This bit near the top is used for running via CLI, the class is below this if block
+# Assume we're doing things interactively if we're passing in args
+if( !empty( $argv[1] ) ) {
+    switch( $argv[1] ) {
+        case "help":
+            $help  = "tCMS Configuration Script Options:\n";
+            $help .= "  install         -- Guides the user through first time tCMS setup.\n";
+            $help .= "  get <FILE>      -- Gets and outputs the values in the supplied JSON file.\n";
+            $help .= "  validate <FILE> -- Compares your config versus the model (sanity check).\n";
+            $help .= "  set <KEY1> <VAL1> <KEY2> <VAL2>... -- Sets the requested config KEY(s) to VALUE(s).\n";
+            echo $help;
+            break;
+        case "install":
+            echo "[INFO] Some of this function is unimplemented -- for now I'm just making things for testing.\n";
+            $user_info = posix_getpwuid();
+            $basedir = $user_info['dir'] . "/.tCMS";
+            if( !file_exists( $basedir ) ) {
+                if( !is_dir( $basdir ) ) {
+                    echo "[FATAL] ~/.tCMS already exists but is not a directory! Stopping here.\n";
+                    exit(1);
+                } else {
+                    $dirs = [ 
+                        $basedir, "$basedir/bin", "$basedir/lib", "$basedir/conf", "$basedir/themes",
+                        "$basedir/microblog", "$basedir/blog", "$basedir/fileshare"
+                    ];
+                    foreach ( $dirs as $dir ) { 
+                        mkdir( $dir );
+                    }
+                }
+                # Handle first time install, assume we're being ran via Makefile
+                $install_base = realpath( dirname( __FILE__ ) );
+                $files2copy = [ "bin/configure-tCMS"];
+                foreach ( $files2copy as $file ) {
+                    if( !copy( "$install_base/$file", "$basedir/$file" ) ) {
+                        echo "[FATAL] $file couldnt' be installed!";
+                        exit(1);
+                    }
+                }
+            } else {
+                echo "[INFO] tCMS appears to already be installed. Nothing to do...\n";
+                exit(0);
+            }
+            # TODO keep going here
+        case "get":
+            if( empty( $argv[2] ) ) {
+                echo "[ERROR] get was passed but no file was passed to get!\n";
+                exit(1);
+            }
+            $config_file = realpath( $argv[2] );
+            $config = configure::get_config_values( $config_file );
+            print_r( $config );
+            break;
+        case "set":
+            echo "[INFO] Unimplemented\n";
+            break;
+        case "validate":
+            if( empty( $argv[2] ) ) {
+                echo "[ERROR] validate was passed but no file was passed to validate against!\n";
+                exit(1);
+            }
+            $config_file = realpath( $argv[2] );
+            $config = configure::get_config_values( $config_file );
+            $valid  = configure::validate_config( $config );
+            echo ( $valid ) ? "[INFO] Config OK\n" : "[ERROR] Config NOT OK.\n";
+            exit( $valid );
+    }
+    exit(0);
+}
+?>

+ 26 - 7
sys/admin/lib/configure.php

@@ -1,21 +1,40 @@
 <?php
-# Assume we're doing things interactively if we're passing in args
-if( !empty( $argv[1] ) ) {
-    exit(0);
-}
 class configure {
     # INPUT:
     #   Param 1: Config file to read. Throws on failure to read.
     #   Param 2: undef or ARRAY of keys you wanna fetch. undef/invalid assumes we'll be wanting all of them.
     # OUTPUT: ARRAY of the requested KEY => VALUE pairs.
-    public function get_config_values() {
-        return;
+    public static function get_config_values( $file = null, $desired_configs = null ) {
+        if( !file_exists( $file ) ) throw new Exception( "$file doesn't exist." );
+        $config = file_get_contents( $file );
+        if( empty( $config ) )      throw new Exception( "$file couldn't be opened or is empty" );
+        $config = json_decode( $config, true );
+        if( empty( $config ) )      throw new Exception( "$file is not valid JSON" );
+        if( !is_array( $config) )   throw new Exception( "Decoded $file's JSON string is not an array" );
+        return $config;
+    }
+    # INPUT: ARRAY, preferably the one returned by get_config_values.
+    # OUTPUT: ARRAY [ result => BOOL, reason => STRING ].
+    public static function validate_config( $config = array() ) {
+        #OK, so here we have to start making some 'assumptions' RE mutli-user environments (cPanel, etc.)
+        $caller_info = posix_getpwuid();
+        $basedir = $caller_info['dir'] . "/.tCMS";
+        if( !file_exists( $basedir) || !is_dir( $basedir ) ) throw new Exception( "~/.tCMS doesn't exist." );
+        $model_file = "$basedir/model.json";
+        $model = self::get_config_values( $model_file );
+        foreach ( $model as $key => $val ) {
+            if( !array_key_exists( $key ) ) {
+                return [ 'result' => 0, 'reason' => "Config file was missing required key '$key'" ];
+            }
+            # TODO check various directories here, make sure we can read/write as appropriate
+        }
+        return 1;
     }
     # INPUT:
     #   Param 1: Config file to write. Throws if it fails to write.
     #   Param 1: ARRAY of KEY => VALUE pairs you wanna set. Throws on invalid input.
     # OUTPUT: BOOL regarding success/failure.
-    public function set_config_values() {
+    public static function set_config_values( $file = null, $desired_configs = null ) {
         return;
     }
 }

+ 11 - 0
t/admin-configure.t

@@ -0,0 +1,11 @@
+<?php
+$basedir = realpath(dirname(__FILE__));
+require_once("$basedir/lib/testmore/testmore.php");
+
+plan(2);
+
+require_ok("$basedir/../sys/admin/lib/configure.php");
+$config = configure::get_config_values("$basedir/../sys/admin/config/main.json.example");
+ok( $config, "configure::get_config_values for known file fetches OK");
+#print_r( $config );
+?>